jiyinyiyong
jiyinyiyong

Reputation: 4713

How can my Node script detect whether it is being called by `require`?

If your code detects that it is called by require, you may want it to branch accordingly, doing or not doing certain things in each case. How do I do this?

Upvotes: 1

Views: 128

Answers (1)

JP Richardson
JP Richardson

Reputation: 39395

Sure, just check if module.parent is not null or undefined. If it is set with an object, then your module is being called with require.

Upvotes: 2

Related Questions