ESM builds should substitute `__filename` and `__dirname` references in CommonJS
See original GitHub issueFeature request
What is the expected behavior?
When building an ESM build in Webpack, free __filename
and __dirname
references in CommonJS files should be supported.
What is motivation or use case for adding/changing the behavior?
See eg https://github.com/vercel/ncc/issues/749 for an ncc bug here.
How should this be implemented in your opinion?
The referencing could do relative path logic or not. That shoudl likely be configurable. In the base case for CommonJS the __filename
value would be the output file __filename
value which seemed to work in some scenarios.
One could just define CJS free __filename
references as fileURLToPath(import.meta.url)
in the Webpack bundle.
Are you willing to work on this yourself? no
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Alternative for __dirname in Node.js when using ES6 modules
Assuming you are working from a module, this solution should work, and also gives you __filename support as well
Read more >Alternatives to __dirname in Node.js with ES modules
The new __dirname and __filename variables created in the code above work just like in CommonJS — if you pass them around, they'll...
Read more >Output - webpack
It is possible to filter out placeholder replacement when you want to use one of the placeholders in the actual file name. For...
Read more >CommonJS to ESM in Node.js - Aral Balkan
Replace. import $1 from $2. While this will do a lot of the work for ... in ESM as what you're exporting are...
Read more >All you need to know to move from CommonJS to ECMAScript ...
Enabling ECMAScript modules (ESM) in Node.js; Syntax; Strict by default ... Recreate missing reference to __filename and __dirname import ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Bump, there’s a PR by @vankop still open https://github.com/webpack/webpack/pull/14247
@fregante , @vankop In my case, I’m bundling everything (except a *.node file) into one
index.bundle.js
, and so node won’t allow usage of__dirname
/__filename
from dependencies since they are now top-level. At least that’s what I think is happening. Correct me if I’m wrong.