nodejs called from module

How can I tell that a script written in node is being called by require?

From the node docs:

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.

You can check property parent of module object. If your module called by require module.parent will return caller module, otherwise module.parent is null.