如何检测我的 node .使用SH:node path-to-file
或JS:require('path-to-file')
调用JS文件?
This is the Node.JS equivalent to my previous question in Perl: How can I run my Perl script only if it wasn't loaded with require?
如何检测我的 node .使用SH:node path-to-file
或JS:require('path-to-file')
调用JS文件?
This is the Node.JS equivalent to my previous question in Perl: How can I run my Perl script only if it wasn't loaded with require?
if (require.main === module) {
console.log('called directly');
} else {
console.log('required as a module');
}
参见此处的文档:https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module