diff --git a/cli/tests/node_compat/config.jsonc b/cli/tests/node_compat/config.jsonc index 81463bcaf5..87530d4f5c 100644 --- a/cli/tests/node_compat/config.jsonc +++ b/cli/tests/node_compat/config.jsonc @@ -366,6 +366,7 @@ "test-http-outgoing-message-inheritance.js", "test-http-outgoing-renderHeaders.js", "test-http-outgoing-settimeout.js", + "test-module-run-main.js", "test-net-access-byteswritten.js", "test-net-better-error-messages-listen-path.js", "test-net-better-error-messages-path.js", diff --git a/cli/tests/node_compat/test/fixtures/run-main.js b/cli/tests/node_compat/test/fixtures/run-main.js new file mode 100644 index 0000000000..9a081cbbae --- /dev/null +++ b/cli/tests/node_compat/test/fixtures/run-main.js @@ -0,0 +1 @@ +globalThis.foo = 42; diff --git a/cli/tests/node_compat/test/parallel/test-module-run-main.js b/cli/tests/node_compat/test/parallel/test-module-run-main.js new file mode 100644 index 0000000000..8e30de2671 --- /dev/null +++ b/cli/tests/node_compat/test/parallel/test-module-run-main.js @@ -0,0 +1,15 @@ +// deno-fmt-ignore-file +// deno-lint-ignore-file + +"use strict"; + +const Module = require("module"); +const assert = require("assert/strict"); +const path = require("path"); + +const file = path.join(__dirname, "..", "fixtures", "run-main.js"); +process.argv = [process.argv[0], file]; +Module.runMain(); + +// The required file via `Module.runMain()` sets this global +assert.equal(globalThis.foo, 42); diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index c73701ba80..a8a70c2fca 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -1108,6 +1108,11 @@ Module.syncBuiltinESMExports = function syncBuiltinESMExports() { throw new Error("not implemented"); }; +// Mostly used by tools like ts-node. +Module.runMain = function () { + Module._load(process.argv[1], null, true); +}; + Module.Module = Module; nativeModuleExports.module = Module;