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/sample
and doing var i18n = require("../i18n");
如果我在〜/ i18nProject /示例中有一个示例应用程序并且执行var i18n = require(“../ i18n”);
The locale
directory 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?
现在我想知道是否有办法可以获得当前运行脚本的路径?
2 个解决方案
#1
14
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将返回名为modules / scripts的名称和路径,请参见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?
如何在node.js中获取调用函数的文件路径?
#2
2
Case 1:
If you want a default locals
directory in the project of the user consuming your npm package, change this line:
如果您想在使用npm包的用户的项目中使用默认的locals目录,请更改以下行:
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.resolve
but I'll let you explore the differences as this is out of the scope of your question.
只需确保运行上述代码的文件位于npm包的根目录中。更改为path.resolve可能更好,但我会让您探讨差异,因为这超出了您的问题的范围。
Case 2:
If you want to access a sample directory in your npm package by using require("../i18n");
, change this line:
如果要使用require(“../ i18n”);来访问npm包中的示例目录,请更改以下行:
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
与案例1相同,请确保在代码上方运行的文件位于npm包的根目录中
#1
14
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将返回名为modules / scripts的名称和路径,请参见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?
如何在node.js中获取调用函数的文件路径?
#2
2
Case 1:
If you want a default locals
directory in the project of the user consuming your npm package, change this line:
如果您想在使用npm包的用户的项目中使用默认的locals目录,请更改以下行:
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.resolve
but I'll let you explore the differences as this is out of the scope of your question.
只需确保运行上述代码的文件位于npm包的根目录中。更改为path.resolve可能更好,但我会让您探讨差异,因为这超出了您的问题的范围。
Case 2:
If you want to access a sample directory in your npm package by using require("../i18n");
, change this line:
如果要使用require(“../ i18n”);来访问npm包中的示例目录,请更改以下行:
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
与案例1相同,请确保在代码上方运行的文件位于npm包的根目录中