[英]Node.js. How to get a path to imported file
I'm writing a parser and i'm getting imports like ../modules/greeting.js
, also i have an absolute path to file (from where i have done an import) - for instance C:\Desktop\Folder\src\scripts\main.js
.我正在写一个解析器,我正在获取像
../modules/greeting.js
这样的导入,我还有一个文件的绝对路径(从我完成导入的地方) - 例如C:\Desktop\Folder\src\scripts\main.js
。
How to get path to file which i have imported?如何获取我导入的文件的路径? (in this case it should be
src/modules/greeting.js
). (在这种情况下,它应该是
src/modules/greeting.js
)。 Thanks!谢谢!
Use this for the absolute path to the imported file:使用它作为导入文件的绝对路径:
path.resolve(path.dirname('C:\\Desktop\\Folder\\src\\scripts\\main.js'), '../modules/greeting.js')
Returns: "C:\Desktop\Folder\src\modules\greeting.js"返回:“C:\Desktop\Folder\src\modules\greeting.js”
Check the API docs for more path utils: https://nodejs.org/api/path.html查看 API 文档以获取更多路径实用程序: https://nodejs.org/api/path.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.