ES6 获取模块内模块的路径 [英] ES6 get path of module inside module
问题描述
如何获取模块内的模块路径?我使用 babel 和 webpack 为浏览器创建包.
How can i get moudle path inside module? I use babel and webpack to create bundle for browsers.
我期待
/src/someModule/index.js
/src/someModule/index.js
console.log(`someModule path is ${process.execPath}`);
在浏览器输出someModule路径是/home/user/proj/src/someModule
或 someModule 路径为/src/someModule
推荐答案
目前还没有办法访问 ES6 模块内的模块路径.这是一个已知问题,还有一个第三阶段提案,用于解析为对象的新元属性 import.meta
与相应的信息.在此处阅读更多相关信息.
There is no way to access the module path inside an ES6 module (yet). This is a known problem, and there's a stage 3 proposal for a new meta property import.meta
that resolves to an object with the respective information. Read more about that here.
也就是说,webpack bundler 确实 支持 Node 的 __dirname
,参见例如webpack 中的当前文件路径 或 webpack 不能使用 __dirname 吗?.
That said, the webpack bundler does support Node's __dirname
, see e.g. Current file path in webpack or Webpack can not use __dirname?.
这篇关于ES6 获取模块内模块的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!