I have a node module that I would like to be callable as a standalone function and also as a library. If called standalone it needs to parse command line arguments and such. If required it needs to, well, not. Can a node file tell how it's called, and if called directly from node <file>
, execute a different code path than if required?
Asked
Active
Viewed 71 times
1

djechlin
- 59,258
- 35
- 162
- 290
-
Related: [node.js equivalent of python's `if __name__ == '__main__'`](http://stackoverflow.com/questions/4981891/node-js-equivalent-of-pythons-if-name-main) – Blender Jun 06 '13 at 16:08
-
See http://stackoverflow.com/questions/13651945/what-is-the-use-of-module-parent-in-node-js-how-can-i-refer-to-the-requireing – Myrne Stol Jun 06 '13 at 16:09
-
@Blender yes, this question should be closed as a dupe of that (but not deleted since it asks in perhaps a more searchable way). – djechlin Jun 06 '13 at 16:12
-
It should be the other way arround with the close votes. Sorry. – TheHippo Jun 06 '13 at 16:13
-
@TheHippo well, no, the answers are over on *that* question. I don't think it's really important that the one with the more general title is the one that is pointed to, but there's probably lots of meta questions that cover this. – djechlin Jun 06 '13 at 16:26