70

I'm just getting started with node.js and I have some experience with Python. In Python I could check whether the __name__ variable was set to "__main__", and if it was I'd know that my script was being run directly. In that case I could run test code or make use of the module directly in other ways.

Is there anything similar in node.js?

alonisser
  • 11,542
  • 21
  • 85
  • 139
Hubro
  • 56,214
  • 69
  • 228
  • 381
  • possible duplicate of [node.js equivalent of python's if \_\_name\_\_ == '\_\_main\_\_'](http://stackoverflow.com/questions/4981891/node-js-equivalent-of-pythons-if-name-main) – David Braun Jun 14 '15 at 21:03

3 Answers3

99

You can use module.parent to determine if the current script is loaded by another script.

e.g.

a.js:

if (!module.parent) {
    console.log("I'm parent");
} else {
    console.log("I'm child");
}

b.js:

require('./a')

run node a.js will output:

I'm parent

run node b.js will output:

I'm child
vdegenne
  • 12,272
  • 14
  • 80
  • 106
qiao
  • 17,941
  • 6
  • 57
  • 46
  • Absolutely perfect. Also looks much cleaner than Python – Hubro Jan 14 '12 at 18:37
  • You can also use `module.main` which is similar – Raynos Jan 14 '12 at 18:45
  • 12
    @Raynos I think you mean `require.main === module` (the official recommendation on http://nodejs.org/api/modules.html). – chbrown Oct 31 '12 at 18:23
  • 4
    Maybe it's just me, but checking `module.parent` seems more intuitive than comparing `require.main` to `module`. – CatDadCode Dec 12 '14 at 22:40
  • 2
    Just FYI, `module.parent` is deprecated as of Node 14.6.0 — see https://nodejs.org/api/deprecations.html#DEP0144. Like https://stackoverflow.com/a/21311382/398713 says, checking `require.main === module` is now the recommended method. – GPX Oct 01 '20 at 05:24
50

The accepted answer is fine. I'm adding this one from the official documentation for completeness:

Accessing the main module

When a file is run directly from Node, require.main is set to its module. That means that you can determine whether a file has been 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.

Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
David Braun
  • 5,573
  • 3
  • 36
  • 42
11

Both options !module.parent and require.main === module work. If you are interested in more details please read my detailed blog post about this topic.

Thorsten Lorenz
  • 11,781
  • 8
  • 52
  • 62
  • 1
    Thanks for noticing, fixed the link. – Thorsten Lorenz Jul 17 '15 at 16:16
  • Just FYI, `module.parent` is deprecated as of Node 14.6.0 — see https://nodejs.org/api/deprecations.html#DEP0144. Like https://stackoverflow.com/a/21311382/398713 says, checking `require.main === module` is now the recommended method. – GPX Oct 01 '20 at 05:26