From 614323fed5538a95758c8dbf6493d696eb7b8043 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 29 Apr 2023 11:30:47 +0530 Subject: [PATCH] sea: allow requiring core modules with the "node:" prefix Previously, the `require()` function exposed to the embedded SEA code was calling the internal `require()` function if the module name belonged to the list of public core modules but the internal `require()` function does not support loading modules with the "node:" prefix, so this change forwards the calls to another `require()` function that supports this. Fixes: https://github.com/nodejs/single-executable/issues/69 Signed-off-by: Darshan Sen --- lib/internal/util/embedding.js | 8 +++++++- test/fixtures/sea.js | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/internal/util/embedding.js b/lib/internal/util/embedding.js index 139d4c7a25fb7c..ac2ae946dedede 100644 --- a/lib/internal/util/embedding.js +++ b/lib/internal/util/embedding.js @@ -36,12 +36,18 @@ function embedderRunCjs(contents) { customDirname); } +let embedderRequireInternal; + function embedderRequire(path) { if (!Module.isBuiltin(path)) { throw new ERR_UNKNOWN_BUILTIN_MODULE(path); } - return require(path); + if (!embedderRequireInternal) { + embedderRequireInternal = Module.createRequire(process.execPath); + } + + return embedderRequireInternal(path); } module.exports = { embedderRequire, embedderRunCjs }; diff --git a/test/fixtures/sea.js b/test/fixtures/sea.js index efdc32708b9898..2ce544cd909cfe 100644 --- a/test/fixtures/sea.js +++ b/test/fixtures/sea.js @@ -10,7 +10,7 @@ expectWarning('ExperimentalWarning', 'might change at any time'); const { deepStrictEqual, strictEqual, throws } = require('assert'); -const { dirname } = require('path'); +const { dirname } = require('node:path'); deepStrictEqual(process.argv, [process.execPath, process.execPath, '-a', '--b=c', 'd']);