@@ -31,8 +31,8 @@ into the `node` binary. During start up, the program checks if anything has been |
31 | 31 | injected. If the blob is found, it executes the script in the blob. Otherwise |
32 | 32 | Node.js operates as it normally does. |
33 | 33 |
|
34 | | -The single executable application feature currently only supports running a |
35 | | -single embedded script using the [CommonJS][] module system. |
| 34 | +The single executable application feature supports running a |
| 35 | +single embedded script using the [CommonJS][] or the [ECMAScript Modules][] module system. |
36 | 36 |
|
37 | 37 | Users can create a single executable application from their bundled script |
38 | 38 | with the `node` binary itself and any tool which can inject resources into the |
@@ -110,6 +110,7 @@ The configuration currently reads the following top-level fields: |
110 | 110 | ```json |
111 | 111 | { |
112 | 112 | "main": "/path/to/bundled/script.js", |
| 113 | + "mainFormat": "commonjs", // Default: "commonjs", options: "commonjs", "module" |
113 | 114 | "executable": "/path/to/node/binary", // Optional, if not specified, uses the current Node.js binary |
114 | 115 | "output": "/path/to/write/the/generated/executable", |
115 | 116 | "disableExperimentalSEAWarning": true, // Default: false |
@@ -290,14 +291,12 @@ This would be equivalent to running: |
290 | 291 | node --no-warnings --trace-exit /path/to/bundled/script.js user-arg1 user-arg2 |
291 | 292 | ``` |
292 | 293 |
|
293 | | -## In the injected main script |
294 | | - |
295 | | -### Single-executable application API |
| 294 | +## Single-executable application API |
296 | 295 |
|
297 | 296 | The `node:sea` builtin allows interaction with the single-executable application |
298 | 297 | from the JavaScript main script embedded into the executable. |
299 | 298 |
|
300 | | -#### `sea.isSea()` |
| 299 | +### `sea.isSea()` |
301 | 300 |
|
302 | 301 | <!-- YAML |
303 | 302 | added: |
@@ -383,25 +382,48 @@ This method can be used to retrieve an array of all the keys of assets |
383 | 382 | embedded into the single-executable application. |
384 | 383 | An error is thrown when not running inside a single-executable application. |
385 | 384 |
|
386 | | -### `require(id)` in the injected main script is not file based |
| 385 | +## In the injected main script |
387 | 386 |
|
388 | | -`require()` in the injected main script is not the same as the [`require()`][] |
389 | | -available to modules that are not injected. It also does not have any of the |
390 | | -properties that non-injected [`require()`][] has except [`require.main`][]. It |
391 | | -can only be used to load built-in modules. Attempting to load a module that can |
392 | | -only be found in the file system will throw an error. |
| 387 | +### Module format of the injected main script |
| 388 | + |
| 389 | +To specify how Node.js should interpret the injected main script, use the |
| 390 | +`mainFormat` field in the single-executable application configuration. |
| 391 | +The accepted values are: |
| 392 | + |
| 393 | +* `"commonjs"`: The injected main script is treated as a CommonJS module. |
| 394 | +* `"module"`: The injected main script is treated as an ECMAScript module. |
| 395 | + |
| 396 | +If the `mainFormat` field is not specified, it defaults to `"commonjs"`. |
| 397 | + |
| 398 | +Currently, `"mainFormat": "module"` cannot be used together with `"useSnapshot"` |
| 399 | +or `"useCodeCache"`. |
| 400 | + |
| 401 | +### Module loading in the injected main script |
| 402 | + |
| 403 | +In the injected main script, module loading does not read from the file system. |
| 404 | +By default, both `require()` and `import` statements would only be able to load |
| 405 | +the built-in modules. Attempting to load a module that can only be found in the |
| 406 | +file system will throw an error. |
393 | 407 |
|
394 | | -Instead of relying on a file based `require()`, users can bundle their |
395 | | -application into a standalone JavaScript file to inject into the executable. |
396 | | -This also ensures a more deterministic dependency graph. |
| 408 | +Users can bundle their application into a standalone JavaScript file to inject |
| 409 | +into the executable. This also ensures a more deterministic dependency graph. |
397 | 410 |
|
398 | | -However, if a file based `require()` is still needed, that can also be achieved: |
| 411 | +To load modules from the file system in the injected main script, users can |
| 412 | +create a `require` function that can load from the file system using |
| 413 | +`module.createRequire()`. For example, in a CommonJS entry point: |
399 | 414 |
|
400 | 415 | ```js |
401 | 416 | const { createRequire } = require('node:module'); |
402 | 417 | require = createRequire(__filename); |
403 | 418 | ``` |
404 | 419 |
|
| 420 | +### `require()` in the injected main script |
| 421 | + |
| 422 | +`require()` in the injected main script is not the same as the [`require()`][] |
| 423 | +available to modules that are not injected. |
| 424 | +Currently, it does not have any of the properties that non-injected |
| 425 | +[`require()`][] has except [`require.main`][]. |
| 426 | + |
405 | 427 | ### `__filename` and `module.filename` in the injected main script |
406 | 428 |
|
407 | 429 | The values of `__filename` and `module.filename` in the injected main script |
@@ -412,6 +434,26 @@ are equal to [`process.execPath`][]. |
412 | 434 | The value of `__dirname` in the injected main script is equal to the directory |
413 | 435 | name of [`process.execPath`][]. |
414 | 436 |
|
| 437 | +### `import.meta` in the injected main script |
| 438 | + |
| 439 | +When using `"mainFormat": "module"`, `import.meta` is available in the |
| 440 | +injected main script with the following properties: |
| 441 | + |
| 442 | +* `import.meta.url`: A `file:` URL corresponding to [`process.execPath`][]. |
| 443 | +* `import.meta.filename`: Equal to [`process.execPath`][]. |
| 444 | +* `import.meta.dirname`: The directory name of [`process.execPath`][]. |
| 445 | +* `import.meta.main`: `true`. |
| 446 | + |
| 447 | +`import.meta.resolve` is currently not supported. |
| 448 | + |
| 449 | +### `import()` in the injected main script |
| 450 | + |
| 451 | +<!-- TODO(joyeecheung): support and document module.registerHooks --> |
| 452 | + |
| 453 | +When using `"mainFormat": "module"`, `import()` can be used to dynamically |
| 454 | +load built-in modules. Attempting to use `import()` to load modules from |
| 455 | +the file system will throw an error. |
| 456 | + |
415 | 457 | ### Using native addons in the injected main script |
416 | 458 |
|
417 | 459 | Native addons can be bundled as assets into the single-executable application |
@@ -599,6 +641,7 @@ start a discussion at <https://github.com/nodejs/single-executable/discussions> |
599 | 641 | to help us document them. |
600 | 642 |
|
601 | 643 | [CommonJS]: modules.md#modules-commonjs-modules |
| 644 | +[ECMAScript Modules]: esm.md#modules-ecmascript-modules |
602 | 645 | [ELF]: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format |
603 | 646 | [Generating single executable preparation blobs]: #1-generating-single-executable-preparation-blobs |
604 | 647 | [Mach-O]: https://en.wikipedia.org/wiki/Mach-O |
|