From 5fd74bfa1c5ed514c3e19fdb2e8590fe251d3ee6 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 11 May 2023 00:13:45 +0200 Subject: [PATCH] feat(node): add `Module.runMain()` (#19080) This PR adds the missing `Module.runMain()` function which is required for tools like `ts-node`. Fixes #19033 --- cli/tests/node_compat/config.jsonc | 1 + cli/tests/node_compat/test/fixtures/run-main.js | 1 + .../test/parallel/test-module-run-main.js | 15 +++++++++++++++ ext/node/polyfills/01_require.js | 5 +++++ 4 files changed, 22 insertions(+) create mode 100644 cli/tests/node_compat/test/fixtures/run-main.js create mode 100644 cli/tests/node_compat/test/parallel/test-module-run-main.js 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;