@@ -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:
@@ -381,25 +380,48 @@ This method can be used to retrieve an array of all the keys of assets
381380embedded into the single-executable application.
382381An error is thrown when not running inside a single-executable application.
383382
384- ### ` require(id) ` in the injected main script is not file based
383+ ## In the injected main script
385384
386- ` require() ` in the injected main script is not the same as the [ ` require() ` ] [ ]
387- available to modules that are not injected. It also does not have any of the
388- properties that non-injected [ ` require() ` ] [ ] has except [ ` require.main ` ] [ ] . It
389- can only be used to load built-in modules. Attempting to load a module that can
390- only be found in the file system will throw an error.
385+ ### Module format of the injected main script
386+
387+ To specify how Node.js should interpret the injected main script, use the
388+ ` mainFormat ` field in the single-executable application configuration.
389+ The accepted values are:
390+
391+ * ` "commonjs" ` : The injected main script is treated as a CommonJS module.
392+ * ` "module" ` : The injected main script is treated as an ECMAScript module.
393+
394+ If the ` mainFormat ` field is not specified, it defaults to ` "commonjs" ` .
395+
396+ Currently, ` "mainFormat": "module" ` cannot be used together with ` "useSnapshot" `
397+ or ` "useCodeCache" ` .
398+
399+ ### Module loading in the injected main script
400+
401+ In the injected main script, module loading does not read from the file system.
402+ By default, both ` require() ` and ` import ` statements would only be able to load
403+ the built-in modules. Attempting to load a module that can only be found in the
404+ file system will throw an error.
391405
392- Instead of relying on a file based ` require() ` , users can bundle their
393- application into a standalone JavaScript file to inject into the executable.
394- This also ensures a more deterministic dependency graph.
406+ Users can bundle their application into a standalone JavaScript file to inject
407+ into the executable. This also ensures a more deterministic dependency graph.
395408
396- However, if a file based ` require() ` is still needed, that can also be achieved:
409+ To load modules from the file system in the injected main script, users can
410+ create a ` require ` function that can load from the file system using
411+ ` module.createRequire() ` . For example, in a CommonJS entry point:
397412
398413``` js
399414const { createRequire } = require (' node:module' );
400415require = createRequire (__filename );
401416```
402417
418+ ### ` require() ` in the injected main script
419+
420+ ` require() ` in the injected main script is not the same as the [ ` require() ` ] [ ]
421+ available to modules that are not injected.
422+ Currently, it does not have any of the properties that non-injected
423+ [ ` require() ` ] [ ] has except [ ` require.main ` ] [ ] .
424+
403425### ` __filename ` and ` module.filename ` in the injected main script
404426
405427The values of ` __filename ` and ` module.filename ` in the injected main script
@@ -410,6 +432,26 @@ are equal to [`process.execPath`][].
410432The value of ` __dirname ` in the injected main script is equal to the directory
411433name of [ ` process.execPath ` ] [ ] .
412434
435+ ### ` import.meta ` in the injected main script
436+
437+ When using ` "mainFormat": "module" ` , ` import.meta ` is available in the
438+ injected main script with the following properties:
439+
440+ * ` import.meta.url ` : A ` file: ` URL corresponding to [ ` process.execPath ` ] [ ] .
441+ * ` import.meta.filename ` : Equal to [ ` process.execPath ` ] [ ] .
442+ * ` import.meta.dirname ` : The directory name of [ ` process.execPath ` ] [ ] .
443+ * ` import.meta.main ` : ` true ` .
444+
445+ ` import.meta.resolve ` is currently not supported.
446+
447+ ### ` import() ` in the injected main script
448+
449+ <!-- TODO(joyeecheung): support and document module.registerHooks -->
450+
451+ When using ` "mainFormat": "module" ` , ` import() ` can be used to dynamically
452+ load built-in modules. Attempting to use ` import() ` to load modules from
453+ the file system will throw an error.
454+
413455### Using native addons in the injected main script
414456
415457Native addons can be bundled as assets into the single-executable application
@@ -597,6 +639,7 @@ start a discussion at <https://github.com/nodejs/single-executable/discussions>
597639to help us document them.
598640
599641[CommonJS]: modules.md#modules-commonjs-modules
642+ [ECMAScript Modules]: esm.md#modules-ecmascript-modules
600643[ELF]: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
601644[Generating single executable preparation blobs]: # generating-single-executable-preparation-blobs
602645[Mach-O]: https://en.wikipedia.org/wiki/Mach-O
0 commit comments