Re:Fetch API has landed into Node.js
by rattray on Tuesday February 01, 2022 @08:44AM
(#30162251)
How do you do both?
[ Parent
| on HN ]
Usually 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.conf...
Helpful, thank you!
It'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.