You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This uses the new StartExecutionCallbackWithModule embedder
API to support ESM entrypoint in SEA via a new configuration
field `"mainFormat"`. The behavior currently aligns with the
embedder API and is mostly in sync with the CommonJS entry
point behavior, except that support for code caching and
snapshot is left for follow-ups.
PR-URL: #61813
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
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>
0 commit comments