How can i get moudle path inside module? I use babel and webpack to create bundle for browsers.
I expect
/src/someModule/index.js
console.log(`someModule path is ${process.execPath}`);
to output in browser someModule path is /home/user/proj/src/someModule
or someModule path is /src/someModule
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.
That said, the webpack bundler does support Node's __dirname
, see e.g. Current file path in webpack or Webpack can not use __dirname?.
The import.meta
is now supported in all modern browsers, yay!
// /es6/someFile.js
console.log(import.meta);
Outputs:
{url: "https://yourdomain.com/es6/someFile.js"}
(even though OP asked about webpack, I believe this info will be useful for many people coming here)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With