@@ -31,8 +31,8 @@ into the `node` binary. During start up, the program checks if anything has been
3131injected. If the blob is found, it executes the script in the blob. Otherwise
3232Node.js operates as it normally does.
3333
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.
3636
3737Users can create a single executable application from their bundled script
3838with 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:
110110``` json
111111{
112112 "main" : " /path/to/bundled/script.js" ,
113+ "mainFormat" : " commonjs" , // Default: "commonjs", options: "commonjs", "module"
113114 "executable" : " /path/to/node/binary" , // Optional, if not specified, uses the current Node.js binary
114115 "output" : " /path/to/write/the/generated/executable" ,
115116 "disableExperimentalSEAWarning" : true , // Default: false
@@ -290,14 +291,12 @@ This would be equivalent to running:
290291node --no-warnings --trace-exit /path/to/bundled/script.js user-arg1 user-arg2
291292```
292293
293- ## In the injected main script
294-
295- ### Single-executable application API
294+ ## Single-executable application API
296295
297296The ` node:sea ` builtin allows interaction with the single-executable application
298297from the JavaScript main script embedded into the executable.
299298
300- #### ` sea.isSea() `
299+ ### ` sea.isSea() `
301300
302301<!-- YAML
303302added:
@@ -383,25 +382,48 @@ This method can be used to retrieve an array of all the keys of assets
383382embedded into the single-executable application.
384383An error is thrown when not running inside a single-executable application.
385384
386- ### ` require(id) ` in the injected main script is not file based
385+ ## In the injected main script
387386
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.
393407
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.
397410
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:
399414
400415``` js
401416const { createRequire } = require (' node:module' );
402417require = createRequire (__filename );
403418```
404419
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+
405427### ` __filename ` and ` module.filename ` in the injected main script
406428
407429The values of ` __filename ` and ` module.filename ` in the injected main script
@@ -412,6 +434,26 @@ are equal to [`process.execPath`][].
412434The value of ` __dirname ` in the injected main script is equal to the directory
413435name of [ ` process.execPath ` ] [ ] .
414436
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+
415457### Using native addons in the injected main script
416458
417459Native 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>
599641to help us document them.
600642
601643[CommonJS]: modules.md#modules-commonjs-modules
644+ [ECMAScript Modules]: esm.md#modules-ecmascript-modules
602645[ELF]: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
603646[Generating single executable preparation blobs]: # 1-generating-single-executable-preparation-blobs
604647[Mach-O]: https://en.wikipedia.org/wiki/Mach-O
0 commit comments