Add one line to a node.js module and it becomes a callable script:
// hello-module.js
module.exports = function() {
return "Hello!";
};
if (!module.parent) module.exports();
Now that module can be called from the command line:
$ node hello-module.js
Hello!
The module.parent
property will be null when the script is called from the command line. When module.parent
is falsey, the module just calls the function which was attached to module.exports
.