I'd like to check if my module is being included or run directly. How can I do this in node.js?
Asked
Active
Viewed 7.8k times
356
-
2`isMain` coming soon to node.js near you :) – Dima Tisnek May 01 '20 at 02:29
-
1@DimaTisnek Source or any more info on `isMain`? It sounds fantastic but I can't find anything about it – Ken Bellows Oct 02 '20 at 12:00
-
1The only significant reference I can find is in [a Gist from CJ Silveiro](https://gist.github.com/ceejbot/b49f8789b2ab6b09548ccb72813a1054) describing NPM's proposal/vision for ESM modules in Node. I haven't been able to find anything official from Node.js themselves. Any links would be appreciated – Ken Bellows Oct 02 '20 at 12:15
-
https://exploringjs.com/nodejs-shell-scripting/ch_nodejs-path.html#detecting-if-module-is-main detecting if the current module is “main” (the app entry point) – aderchox Apr 20 '23 at 10:51
2 Answers
467
The nodejs docs describe another way to do this which may be the preferred method:
When a file is run directly from Node, require.main is set to its module.
To take advantage of this, check if this module is the main module and, if so, call your main code:
function myMain() {
// main code
}
if (require.main === module) {
myMain();
}
EDIT: If you use this code in a browser, you will get a "Reference error" since "require" is not defined. To prevent this, use:
if (typeof require !== 'undefined' && require.main === module) {
myMain();
}

Shlomo
- 120
- 2
- 8

Stephen Emslie
- 10,539
- 9
- 32
- 28
-
18you always have to check require.main === module **irrespective** of your function name. To make it clear above code should be modified as: `var fnName = function(){ // code } if (require.main === module) { fnName(); }` – Kunal Kapadia May 29 '15 at 12:47
-
-
4@OhadCohen "try...catch" might also catch real errors. I think it is better to just check if typeof require != 'undefined'. – Erel Segal-Halevi Apr 27 '17 at 19:17
-
-
4
-
2@seamlik in this case, the package [es-main](https://www.npmjs.com/package/es-main) can be used. Usage: `esMain(import.meta)` returns true iff the file is not imported. – Charlie Harding May 13 '21 at 12:05
-
For the deno users that came here https://deno.land/manual@v1.0.0/examples/testing_if_main – Norfeldt Oct 22 '21 at 10:08
69
if (!module.parent) {
// this is the main module
} else {
// we were require()d from somewhere else
}
EDIT: If you use this code in a browser, you will get a "Reference error" since "module" is not defined. To prevent this, use:
if (typeof module !== 'undefined' && !module.parent) {
// this is the main module
} else {
// we were require()d from somewhere else or from a browser
}

nornagon
- 15,393
- 18
- 71
- 85
-
-
11Nope, but it's used in one of [node.js's tests](https://github.com/joyent/node/blob/master/test/simple/test-cli-eval.js) – nornagon May 19 '11 at 08:52
-
1To me this reads better than the accepted answer and has the benefit of not requiring the module's "name" – blented Sep 20 '13 at 18:47
-
12
-
2
-
1@pcnate Could you consider adding another answer to explain how to use `module.main`? – tsh Nov 11 '21 at 02:24