如何获取在nodejs中运行脚本的文件的当前路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20641870/
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
How to get current path of the file that run the script in nodejs
提问by Ali
So I have node modules that can be require for Internationalization.
所以我有国际化可能需要的节点模块。
I'm trying to get the current path of the file that run my node module inside the nodule module.
我正在尝试获取在结节模块内运行我的节点模块的文件的当前路径。
Use case #1:
用例#1:
Inside ~/yourProject/node_modules/i18n.js
里面 ~/yourProject/node_modules/i18n.js
var current_path_to_locales_dir = path.join(__dirname, "locale");
And the path of the server is:
服务器的路径是:
~/YourUserName/yourProject/app.js
Doing var i18n = require("i18n");
正在做 var i18n = require("i18n");
And trying to get the path it will return
并试图获得它将返回的路径
/User/YourUserName/yourProject/node_modules/locale
Which is correct but I'm expecting it to look for
这是正确的,但我期待它寻找
/User/YourUserName/yourProject/locale
Use case #2:
用例#2:
Inside ~/i18nProject/i18n.js
里面 ~/i18nProject/i18n.js
var current_path_to_locales_dir = path.join(__dirname, "locale");
If I have a sample app in the ~/i18nProject/sampleand doing var i18n = require("../i18n");
如果我在~/i18nProject/sample做一个示例应用程序var i18n = require("../i18n");
The localedirectory this time will be
这次的locale目录是
/User/YourUserName/i18nProject/locale
Again the above is correct but I would expect it to be
以上再次是正确的,但我希望它是
/User/i18nProject/sample/locale/
Now I'm wondering if there is a way that I can get the path of the current running script?
现在我想知道是否有办法获取当前正在运行的脚本的路径?
回答by Nitin...
Use the variables __filename, __dirname will return called modules/scripts name and path, see here http://nodejs.org/docs/latest/api/globals.html#globals_filename
使用变量 __filename, __dirname 将返回调用的模块/脚本名称和路径,请参见此处http://nodejs.org/docs/latest/api/globals.html#globals_filename
Update
更新
In your case you need to get the caller of module/script:
在您的情况下,您需要获取模块/脚本的调用者:
Node.js does not do it for you, so technically you may have to add extra wrapper and parameter to your module and convert it into a function that accepts this information.
Node.js 不会为您做这件事,因此从技术上讲,您可能必须向模块添加额外的包装器和参数,并将其转换为接受此信息的函数。
However, there is a work around to implement getCaller by using Error.prepareStackTrace This thread already has your solution:
但是,有一种解决方法可以通过使用 Error.prepareStackTrace 来实现 getCaller 这个线程已经有了你的解决方案:
How can one get the file path of the caller function in node.js?
回答by srquinn
Case 1:
情况1:
If you want a default localsdirectory in the project of the user consuming your npm package, change this line:
如果您希望locals在使用您的 npm 包的用户的项目中有一个默认目录,请更改以下行:
var current_path_to_locales_dir = path.join(__dirname, "locale");
to:
到:
var current_path_to_locales_dir = path.join(__dirname, "..", "locale");
// The ".." moves back one directory out of node_modules.
Just be sure that the file running the above code is in the root of your npm package. It might be better to change to path.resolvebut I'll let you explore the differences as this is out of the scope of your question.
只需确保运行上述代码的文件位于 npm 包的根目录中。更改为可能会更好,path.resolve但我会让您探索差异,因为这超出了您的问题范围。
Case 2:
案例2:
If you want to access a sample directory in your npm package by using require("../i18n");, change this line:
如果要使用 访问 npm 包中的示例目录,请require("../i18n");更改以下行:
var current_path_to_locales_dir = path.join(__dirname, "locale");
to:
到:
var current_path_to_locales_dir = path.join(__dirname, "sample", "locale");
Same as case 1, make sure the file running above code is in the root of your npm package
同case 1,确保上面代码运行的文件在你的npm包的根目录下

