Every file of a package is an ES module. Scripts reach Asterobot's functions, their own files and other packages through import statements, and through nothing else.
The built-in modules
| Module | Exports | Reference |
|---|---|---|
asterobot:protocol |
send, request, wait, on, off, onTraffic, intercept |
asterobot:protocol |
asterobot:bot |
session, currentMap, disconnect, botDebug, botInfo, botWarn, botError |
asterobot:bot |
asterobot:timers |
sleep |
asterobot:timers |
asterobot:gamedata |
text, record, table, find, search, query |
asterobot:gamedata |
asterobot:console |
console, debug, log, info, warn, error |
asterobot:console |
asterobot:runtime |
packages, whose packages.info() describes a package |
asterobot:runtime |
asterobot:parameters |
values, onChange, offChange |
asterobot:parameters |
asterobot:pathfinding |
findPath, cellToPoint, pointToCell, distance, direction |
asterobot:pathfinding |
asterobot:movement |
move |
asterobot:movement |
These modules only have named exports. Import each function by its name:
import { on, send } from "asterobot:protocol";
import { record, text } from "asterobot:gamedata";
There's no default export and no object grouping a module's functions: import gamedata from "asterobot:gamedata" and import { gamedata } from "asterobot:gamedata" both fail. If you prefer one name for a module, use a namespace import: import * as protocol from "asterobot:protocol", then protocol.send(...). asterobot:console is the one module that also exports an object, console, so that console.log() reads the way it does elsewhere.
asterobot:parameters is scoped to the package that imports it. In your package, values holds your package's settings. Imported from a dependency, it holds the dependency's own. Settings in dependencies explains how those show on the bot's page.
A built-in name that doesn't exist, such as asterobot:fs, keeps the script from loading. The reason contains built-in module "asterobot:fs" is not registered.
Note
Coming soon. Asterobot has room for extension modules, named asterobot:extension/ followed by a name, which would add functions of their own. None is available yet, so importing one fails. See Extension modules.
Importing your own files
// index.js
import { formatChat } from "./lib/format.js";
// lib/format.js
export function formatChat(channel, sender, content) {
return `[${channel}] ${sender}: ${content}`;
}
A relative import follows these rules:
- It starts with
./or../, and is resolved from the folder of the importing file. - It names the file completely, extension included:
./lib/format.js, not./lib/format. - It stays inside the package.
../can't lead to another package's files. - It never starts with
/and never contains a backslash.
A path without ./, such as lib/format.js, isn't a file path: Asterobot reads it as the name of a package your package depends on, and fails with has no locked dependency for "lib/format.js".
When an import breaks a rule, the script doesn't load, and the toast gives no reason. The reason contains one of these texts:
| Problem | Error |
|---|---|
| The file doesn't exist, or the extension is missing | module "library/my-first-script/1.0.0/lib/format" imported by "library/my-first-script/1.0.0/index.js" is not installed |
| The path leaves the package | relative import "../other.js" escapes package root "library/my-first-script/1.0.0" |
The path starts with / |
absolute module specifier "/lib/format.js" is forbidden |
The editor underlines an import of a file that doesn't exist as an error, and the bot's Settings > Package settings shows the full reason, as The entry function explains.
Each module runs once per start, however many files import it, so a variable at the top level of lib/format.js is shared by every file that imports it. Each bot has its own copy: two bots playing the same package never share a variable.
Importing another package
To import another package from your library, first declare it in the dependencies of your asterobot.json, as Dependencies describes. Then import it by the name you declared:
import { formatChat } from "chat-tools";
import { findSpots } from "@alice:mining";
You receive what the dependency's index.js exports, and only that. A path into the dependency, such as "chat-tools/lib/format.js", isn't supported and fails with has no locked dependency for "chat-tools/lib/format.js". A package that wants to share something exports it from its index.js.
A name your package doesn't declare fails the same way. That's also what happens with "lodash", "fs" or any other npm or Node.js module name.
Versioned imports
An import can carry a version after an @:
| Import | What it picks |
|---|---|
"chat-tools@1.2.0" |
Version 1.2.0 of chat-tools. When your manifest declares two versions of the same package side by side, this is how you pick one. |
"chat-tools@^1.0.0" |
The highest version of chat-tools that matches the range |
"chat-tools@latest" |
The highest version of chat-tools |
A range or latest only chooses among the versions of that package that are already part of what the bot runs, brought in by your manifest or by a dependency's. It never installs anything. When none matches, the script doesn't load, and the reason contains no installed version of "chat-tools" satisfies "^1.0.0".
What a script can't import
| Not possible | What happens |
|---|---|
import() called as a function |
The call rejects when it runs, with dynamic import() is not supported by this Asterobot runtime. The editor warns about it. |
require() |
It doesn't exist, and calling it throws a ReferenceError. |
| npm packages and Node.js modules | There's no npm. A name like "lodash" is read as an undeclared dependency. |
| JSON, text or image files | A package only holds .js and .mjs modules, so there's no such file to import. |
No browser or Node.js globals
Scripts have the standard JavaScript built-ins, such as Math, JSON, Date, Map, Promise and BigInt, and the functions of the asterobot: modules. The globals of a browser or of Node.js don't exist:
| Global you might expect | In a script |
|---|---|
console |
Import botInfo() and the rest from asterobot:bot, or console from asterobot:console. |
setTimeout(), setInterval(), setImmediate() |
await sleep(milliseconds) from asterobot:timers |
fetch(), XMLHttpRequest, WebSocket |
Not available |
window, document, localStorage |
Not available |
process, Buffer, require, __dirname |
Not available |
structuredClone(), TextEncoder, TextDecoder, queueMicrotask() |
Not available |
The editor knows this: it marks these names as errors. JavaScript support lists the language features scripts have and the few they don't.
Next, Async and timing.
Aucun avis à afficher.