4 ms·
I don't speak for the project a whole :) Node has supported ESModules (`import`) for a while and you can use it (either by naming your files `.mjs` or setting
by inglor 5y ago
I don't speak for the project a whole :)
Node has supported ESModules (`import`) for a while and you can use it (either by naming your files `.mjs` or setting `type: module` in your package.json.
There is a solid interop story between require/import and you can mix if you want.
`require` is going to be supported "forever" so code doesn't break and both module systems work.
As for the general question: Node is committed to supporting modern JavaScript and to not allow such a gap to be created. If you see a place Node doesn't do a great job at that please tell us!
- arcatek 5y agoHowever note that ESM in Node comes with drawbacks that prevent end-users from relying them in various situations. Those will be mostly solved once loaders become stable, but until then it's still advised to ship packages as both CJS and ESM.
- rattray 5y agoHow do you do both?
- inglor 5y agoIt's very easy to re-export ESM (import) as CJS (require) and vice-versa. The main issue is that ESM by default For example to use `require` inside ESModules you would do: ```mjs import { createRequire } from 'module'; const require = createRequire(import.meta.url); require('./whatever-in-cjs'); ``` There is a reason this isn't "by default" though since ESM doesn't "silently" interop with CJS to not make writing universal code harder.
- arcatek 5y agoUsually you setup a transpiler to target both. For example, my packages are bundled with rollup towards both cjs & esm: https://github.com/arcanis/clipanion/blob/master/rollup.config.js#L6-L17 https://github.com/arcanis/clipanion/blob/master/rollup.conf...
- rattray 5y agoHelpful, thank you!