ES6 获取模块内模块的路径 [英] ES6 get path of module inside module

查看:31
本文介绍了ES6 获取模块内模块的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取模块内的模块路径?我使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆