javascript Nodejs:获取调用函数的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16697791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:40:28  来源:igfitidea点击:

Nodejs: get filename of caller function

javascriptnode.js

提问by einstein

I wonder how-to get an absolute path of a caller of a function?

我想知道如何获得函数调用者的绝对路径?

Let say that:

让我们说:

in file a.jsI call b(); b()is a function defined in file b.js. a.jsrequires b. So how do I get a.jsabsolute path from b.jsin node?

a.js我调用的文件中b()b()是文件中定义的函数b.jsa.js需要b. 那么如何a.jsb.js节点中获取绝对路径?

回答by Rhionin

Failing to restore the prepareStackTrace function can cause issues. Here's an example that removes side-effects

未能恢复 prepareStackTrace 函数可能会导致问题。这是一个消除副作用的示例

function _getCallerFile() {
    var originalFunc = Error.prepareStackTrace;

    var callerfile;
    try {
        var err = new Error();
        var currentfile;

        Error.prepareStackTrace = function (err, stack) { return stack; };

        currentfile = err.stack.shift().getFileName();

        while (err.stack.length) {
            callerfile = err.stack.shift().getFileName();

            if(currentfile !== callerfile) break;
        }
    } catch (e) {}

    Error.prepareStackTrace = originalFunc; 

    return callerfile;
}

回答by Bagus Budiarto

This is an example how to use stacktrace to find caller file in node

这是一个如何使用堆栈跟踪在节点中查找调用者文件的示例

function _getCallerFile() {
    try {
        var err = new Error();
        var callerfile;
        var currentfile;

        Error.prepareStackTrace = function (err, stack) { return stack; };

        currentfile = err.stack.shift().getFileName();

        while (err.stack.length) {
            callerfile = err.stack.shift().getFileName();

            if(currentfile !== callerfile) return callerfile;
        }
    } catch (err) {}
    return undefined;
}

回答by imme

Not exactly answering the question here but some might appreciate this information.

不完全回答这里的问题,但有些人可能会欣赏这些信息。

With NodeJS & Forever(-monitor), the following contains a filename from which the process was started:

使用 NodeJS & Forever(-monitor),以下内容包含启动进程的文件名:

process.mainModule.filename

Haven't tried many uses? though.

没有尝试过很多用途吗?尽管。

This seems to be a pretty decent explanation: https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi.

这似乎是一个相当不错的解释:https: //code.google.com/p/v8/wiki/JavaScriptStackTraceApi

回答by Tim Long

The npm caller packagehas a function that returns the path and filename of the callers.

npm caller 包有一个函数可以返回调用者的路径和文件名。

回答by cancerbero

Use https://github.com/sindresorhus/callsites

使用https://github.com/sindresorhus/callsites

If you use the first answer you could mess up with other libraries trying to do the same thing. See for example: https://github.com/facebook/jest/issues/5303

如果您使用第一个答案,您可能会与其他试图做同样事情的图书馆搞混。参见例如:https: //github.com/facebook/jest/issues/5303

回答by Halcyon

Getting a stacktrace in JavaScript is quite hard. The best method I've found is to throw an Error, catch it, get the stack from Error.getStack()(not implemented in all browsers, that means you IE.) and formatting the output.

在 JavaScript 中获取堆栈跟踪非常困难。我发现的最好方法是抛出一个错误,捕获它,从中获取堆栈Error.getStack()(并非在所有浏览器中都实现,这意味着您是 IE。)并格式化输出。

Each stack frame gives you a filepath, line number and function name. Webkit even has support for arguments but that wasn't working yet when I last checked.

每个堆栈帧都为您提供文件路径、行号和函数名称。Webkit 甚至支持参数,但在我上次检查时还没有工作。

Then there is the problem of tracing code across different events.

然后是跨不同事件跟踪代码的问题。

I actually wrote a blog post about this: http://fritsvancampen.wordpress.com/2013/03/28/error-handling-in-javascript-a-better-way/

我实际上写了一篇关于这个的博客文章:http: //fritsvancampen.wordpress.com/2013/03/28/error-handling-in-javascript-a-better-way/

回答by basilikum

You can use require.resolve(module)to determine the full path of a module:

您可以使用require.resolve(module)来确定模块的完整路径:

var path = require.resolve("a");

//or

var path = require.resolve("./a.js");