Skip to content

Commit

Permalink
feat(node): add Module.runMain() (#19080)
Browse files Browse the repository at this point in the history
This PR adds the missing `Module.runMain()` function which is required
for tools like `ts-node`.

Fixes #19033
  • Loading branch information
marvinhagemeister authored and dsherret committed May 11, 2023
1 parent 610936f commit 56a9a2a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions cli/tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions cli/tests/node_compat/test/fixtures/run-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
globalThis.foo = 42;
15 changes: 15 additions & 0 deletions cli/tests/node_compat/test/parallel/test-module-run-main.js
Original file line number Diff line number Diff line change
@@ -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);
5 changes: 5 additions & 0 deletions ext/node/polyfills/01_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 56a9a2a

Please sign in to comment.