Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing require(esm) via transpilation #162

Open
GeoffreyBooth opened this issue Sep 16, 2023 · 1 comment
Open

Implementing require(esm) via transpilation #162

GeoffreyBooth opened this issue Sep 16, 2023 · 1 comment

Comments

@GeoffreyBooth
Copy link
Member

It occurs to me that someone could write a package that uses the module customization hooks to detect when someone does require of an ESM package and then that package gets transpiled into CommonJS on demand. So instead of pinning node-fetch to an old version, say, you could use the latest version and have --import transpile-esm-dependencies automatically convert it on the fly into a sync CommonJS version that an app can use. This would solve one of the more frustrating annoyances that users have faced since the ESM migration began.

This isn’t quite what Bun does automatically with its require(esm), since it transpiles everything into ESM that it mostly runs sync; but it’s the same from the perspective of the user. What I’m describing is essentially what Webpack does when you tell it to transpile your node_modules or certain packages inside node_modules.

Does a library exist to do what I’m suggesting here? If so, or if someone wants to write one, we could maybe mention it from the Node docs. I know we’re reluctant to recommend particular libraries in our docs, but this problem comes up so often that it would be nice if we could tell users how to solve it. I don’t foresee Node having some built-in solution; we’re not going to ship a bundler within Node anytime soon. But I think we should be able to get consensus on providing some guidance to users for common issues like this, as well as guiding them on how to get runtime TypeScript transpilation working.

@ljharb
Copy link
Member

ljharb commented Sep 16, 2023

isn’t that what npmjs.com/esm does?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants