Skip to content

module: add module.entrypoint - #64903

Open
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:module-entrypoint
Open

module: add module.entrypoint#64903
mcollina wants to merge 2 commits into
nodejs:mainfrom
mcollina:module-entrypoint

Conversation

@mcollina

@mcollina mcollina commented Aug 1, 2026

Copy link
Copy Markdown
Member

Adds module.entrypoint to node:module: the resolved URL string of the entry point of the current thread.

The semantics follow require.main and import.meta.main (thread-scoped): inside a worker thread it is the URL of the script the worker was started with, not the entry point of the process. This complements #64800, which exposes the process-wide entry point inherited by workers.

  • Works regardless of whether the entry point is CommonJS or ESM.
  • undefined for --eval, the REPL, STDIN input, and eval: true workers; set to the data: URL for data URL workers.
  • The value is fully resolved: extension searching is applied and symlinks are followed (unless --preserve-symlinks-main), so it matches require.main.filename / the entry point's import.meta.url rather than process.argv[1].

Refs: #51840
Refs: #64800

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Aug 1, 2026
@codecov

codecov Bot commented Aug 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.30%. Comparing base (a7d16a8) to head (fa6904f).
⚠️ Report is 95 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64903      +/-   ##
==========================================
+ Coverage   90.13%   90.30%   +0.17%     
==========================================
  Files         746      759      +13     
  Lines      242893   247629    +4736     
  Branches    45769    46683     +914     
==========================================
+ Hits       218935   223631    +4696     
- Misses      15444    15459      +15     
- Partials     8514     8539      +25     
Files with missing lines Coverage Δ
lib/internal/main/worker_thread.js 94.37% <100.00%> (+0.01%) ⬆️
lib/internal/modules/helpers.js 98.96% <100.00%> (+0.02%) ⬆️
lib/internal/modules/run_main.js 97.71% <100.00%> (+0.06%) ⬆️
lib/module.js 100.00% <100.00%> (ø)

... and 107 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@avivkeller

Copy link
Copy Markdown
Member

@mcollina lint?

Add `module.entrypoint`, a property exposing the resolved URL of the
entry point of the current thread. Unlike `require.main`, it works
regardless of whether the entry point is a CommonJS or an ECMAScript
module, and inside worker threads it reflects the entry point of the
worker itself, matching the semantics of `require.main` and
`import.meta.main`.

Refs: nodejs#51840
Refs: nodejs#64800
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the module-entrypoint branch from b6075b5 to d5b61b2 Compare August 3, 2026 13:35
@richardlau richardlau added the semver-minor PRs that contain new features and should be released in the next minor version. label Aug 3, 2026
@avivkeller avivkeller added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Aug 4, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment on lines +14 to +18
function fixtureURL(...args) {
// The entrypoint is fully resolved, so account for symlinks in the path
// to the fixtures directory.
return pathToFileURL(realpathSync(fixtures.path('module-entrypoint', ...args))).href;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use fixtures.fileURL?

Comment thread test/parallel/test-module-entrypoint.js Outdated
Comment thread doc/api/module.md
Comment on lines +96 to +100
import { entrypoint } from 'node:module';

if (import.meta.url === entrypoint) {
console.log('This module is the entry point of the current thread');
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a great example, import.meta.main would be a much better choice here – and if we can't find a good example, do we actually need that API?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because import.meta.main only returns true if it's the current one, but don't report us what's the actual "entrypoint" of the current thread/module chain. So it's not easily discoverable.

The same can be said for #64800.

If you want I can remove the example.

@aduh95 aduh95 Aug 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think the example either needs to include import.meta.main, and/or process.mainModule (to explain the diffs and show some actual use-cases) otherwise it looks like we're adding a less elegant way to do something that's already possible

Address review feedback from aduh95.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants