diff --git a/doc/api/module.md b/doc/api/module.md index 956b10942ba3..7f4c98e23ed6 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -66,6 +66,49 @@ const require = createRequire(import.meta.url); const siblingModule = require('./sibling-module'); ``` +### `module.entrypoint` + + + +* Type: {string|undefined} + +The resolved URL of the entry point of the current thread, or `undefined` when +Node.js was started without an entry point script (`--eval`, the REPL, or code +piped via STDIN). It works regardless of whether the entry point is a +CommonJS or an ECMAScript module. + +Inside a [worker thread][], `module.entrypoint` is the URL of the script the +worker was started with, matching the semantics of `require.main` and +[`import.meta.main`][], rather than the entry point of the process. Workers +created with `eval: true` have an `undefined` entrypoint. + +Unlike `process.argv[1]`, the value is fully resolved: extension searching is +applied, and symlinks are followed unless [`--preserve-symlinks-main`][] is +set. This makes it consistent with the `require.main.filename` of a CommonJS +entry point and the `import.meta.url` of an ECMAScript module entry point. + +The value is `undefined` in code that runs before the entry point has been +resolved, such as modules preloaded with `--require`. + +```mjs +import { entrypoint } from 'node:module'; + +if (import.meta.url === entrypoint) { + console.log('This module is the entry point of the current thread'); +} +``` + +```cjs +const { entrypoint } = require('node:module'); +const { pathToFileURL } = require('node:url'); + +if (pathToFileURL(__filename).href === entrypoint) { + console.log('This module is the entry point of the current thread'); +} +``` + ### `module.findPackageJSON(specifier[, base])`