Detect if called through require or directly by command line

后端 未结 5 1021
南方客
南方客 2020-11-30 16:26

How can I detect whether my Node.JS file was called using SH:node path-to-file or JS:require(\'path-to-file\')?

This is the Node.JS equival

5条回答
  •  一生所求
    2020-11-30 17:13

    I always find myself trying to recall how to write this goddamn code snippet, so I decided to create a simple module for it. It took me a bit to make it work since accessing caller's module info is not straightforward, but it was fun to see how it could be done.

    So the idea is to call a module and ask it if the caller module is the main one. We have to figure out the module of the caller function. My first approach was a variation of the accepted answer:

    module.exports = function () {
        return require.main === module.parent;
    };
    

    But that is not guaranteed to work. module.parent points to the module which loaded us into memory, not the one calling us. If it is the caller module that loaded this helper module into memory, we're good. But if it isn't, it won't work. So we need to try something else. My solution was to generate a stack trace and get the caller's module name from there:

    module.exports = function () {
        // generate a stack trace
        const stack = (new Error()).stack;
        // the third line refers to our caller
        const stackLine = stack.split("\n")[2];
        // extract the module name from that line
        const callerModuleName = /\((.*):\d+:\d+\)$/.exec(stackLine)[1];
    
        return require.main.filename === callerModuleName;
    };
    

    Save this as is-main-module.js and now you can do:

    const isMainModule = require("./is-main-module");
    
    if (isMainModule()) {
        console.info("called directly");
    } else {
        console.info("required as a module");
    }
    

    Which is easier to remember.

提交回复
热议问题