Feature: Mix CommonJS and ESM like Bun.js #49664
-
The way Bun.js removes the entire problem of CommonJS vs. ESM is so beautiful, by letting you mix them inside the same file. What is the reason why we can't have that with NodeJS? Then all packages would just work everywhere, and we can remove the This feature alone is making me want to switch away from NodeJS, there are already a few packages that are incompatible with my CommonJS projects, which also means I'm missing out on security updates. There is no way I will ever spend time rewriting all my old code, it's just not possible. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
If you go over to the nodejs/modules repo you'll see this is a discussion that's been litigated to death but to tl;dr it: bun picked an 80/20 solution, node didn't. |
Beta Was this translation helpful? Give feedback.
-
sorry, but it is super confusing what will nodejs do to CJS and ESM. some say there is no plan to remove CJS. |
Beta Was this translation helpful? Give feedback.
-
Now that NodeJS supports require of ESM packages (experimental in v.22), the need for ESM is completely gone because we can use ESM-only packages with CJS. I'd say this is resolved for now. https://joyeecheung.github.io/blog/2024/03/18/require-esm-in-node-js/ |
Beta Was this translation helpful? Give feedback.
It's spread out over a lot of issues, there's no single discussion I can link you to, but the crux is this: you cannot with 100% certainty determine if JS code is CJS or ESM. What you can do is let loose some heuristics and get it right most of the time but wrong some of the time (bun), or make it explicit (node.)
Bun's take-up is currently a rounding error compared to node and therefore the downsides to making an educated guess aren't very apparent yet but I fully expect that if and when it gets popular, this particular decision will come back to bite it in the, ah, buns.