Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: document that require.main may be undefined #41384

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ run directly by testing `require.main === module`.
For a file `foo.js`, this will be `true` if run via `node foo.js`, but
`false` if run by `require('./foo')`.

Because `module` provides a `filename` property (normally equivalent to
`__filename`), the entry point of the current application can be obtained
by checking `require.main.filename`.
When the entry point is not a CommonJS module, `require.main` is `undefined`,
and the main module is out of reach. You can try to make assumptions based on
`process.cwd()` and `process.argv[1]` values to figure out the entry point of
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
the current application.
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

## Package manager tips

Expand Down Expand Up @@ -718,10 +719,11 @@ extensions gets slower with each registered extension.
added: v0.1.17
-->

* {module}
* {module | undefined}

The `Module` object representing the entry script loaded when the Node.js
process launched.
process launched, or `undefined` if the entry point of the program is not a
CommonJS module.
See ["Accessing the main module"](#accessing-the-main-module).

In `entry.js` script:
Expand Down