Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 get path of module inside module

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

like image 857
Andrew Avatar asked Dec 22 '17 08:12

Andrew


2 Answers

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?.

like image 158
Bergi Avatar answered Oct 05 '22 11:10

Bergi


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)

like image 31
Klesun Avatar answered Oct 05 '22 12:10

Klesun