question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ESM builds should substitute `__filename` and `__dirname` references in CommonJS

See original GitHub issue

Feature 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:open
  • Created 2 years ago
  • Reactions:8
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fregantecommented, Oct 5, 2022

Bump, there’s a PR by @vankop still open https://github.com/webpack/webpack/pull/14247

1reaction
johnmankocommented, Jul 6, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found