From 04a81b3e21ae6a357625f185bcf231ee797b87c9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 5 Jan 2022 11:05:25 +0100 Subject: [PATCH] doc: document that `require.main` may be `undefined` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/41384 Reviewed-By: Guy Bedford Reviewed-By: Geoffrey Booth Reviewed-By: Adrian Estrada Reviewed-By: Tobias Nießen Reviewed-By: Michaël Zasso Reviewed-By: Anto Aravinth --- doc/api/modules.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/api/modules.md b/doc/api/modules.md index c1be914ca90bf0..63629350d49503 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -74,9 +74,8 @@ 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. ## Package manager tips @@ -716,10 +715,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: