sea: add test and update docs for import() with code cache

Signed-off-by: Ali Hassan <ali-hassan27@outlook.com>
PR-URL: https://github.com/nodejs/node/pull/62678
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
Ali Hassan authored and GitHub committed 2026-09-27 06:56:43 +00:00
1 parent cf0434f59c
commit 7d79ed37de
5 files changed
+56 -10

No files matched your search

+3 -4
View File
@@ -352,8 +352,6 @@ executable application is launched, instead of compiling the `main` script from
scratch, Node.js would use the code cache to speed up the compilation, then
execute the script, which would improve the startup performance.
**Note:** `import()` does not work when `useCodeCache` is `true`.
### Execution arguments
The `execArgv` field can be used to specify Node.js-specific
@@ -587,8 +585,9 @@ injected main script with the following properties:
<!-- TODO(joyeecheung): support and document module.registerHooks -->
When using `"mainFormat": "module"`, `import()` can be used to dynamically
load built-in modules. Attempting to use `import()` to load modules from
`import()` can be used to dynamically load built-in modules in both
CommonJS and ESM (`"mainFormat": "module"`) single executable applications.
Attempting to use `import()` to load modules from
the file system will throw an error.
### Using native addons in the injected main script
-6
View File
@@ -752,12 +752,6 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path,
Local<UnboundModuleScript> unbound = module->GetUnboundModuleScript();
cache.reset(ScriptCompiler::CreateCodeCache(unbound));
} else {
// TODO(RaisinTen): Using the V8 code cache prevents us from using
// `import()` in the SEA code. Support it. Refs:
// https://github.com/nodejs/node/pull/48191#discussion_r1213271430
// TODO(joyeecheung): this likely has been fixed by
// https://chromium-review.googlesource.com/c/v8/v8/+/5401780 - add a test
// and update docs.
LocalVector<String> parameters(
isolate,
{
@@ -0,0 +1,6 @@
{
"main": "sea.js",
"output": "sea",
"useCodeCache": true,
"disableExperimentalSEAWarning": true
}
+13
View File
@@ -0,0 +1,13 @@
(async () => {
const assert = require('node:assert');
// Dynamic import of a built-in module should work even with code cache.
const { strictEqual } = await import('node:assert');
assert.strictEqual(strictEqual, assert.strictEqual);
// Dynamic import of another built-in module.
const { join } = await import('node:path');
assert.strictEqual(typeof join, 'function');
console.log('dynamic import with code cache works');
})();
@@ -0,0 +1,34 @@
'use strict';
// This tests that import() works in a CJS single executable application
// when useCodeCache is true.
require('../common');
const {
buildSEA,
skipIfBuildSEAIsNotSupported,
} = require('../common/sea');
skipIfBuildSEAIsNotSupported();
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const { spawnSyncAndAssert } = require('../common/child_process');
tmpdir.refresh();
const outputFile = buildSEA(fixtures.path('sea', 'use-code-cache-dynamic-import'));
spawnSyncAndAssert(
outputFile,
[],
{
env: {
NODE_DEBUG_NATIVE: 'SEA',
...process.env,
},
},
{
stdout: 'dynamic import with code cache works\n',
});