top | item 30162166 (no title) arcatek | 4 years ago However 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. discuss order hn newest rattray|4 years ago How do you do both? arcatek|4 years ago 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... load replies (1) inglor|4 years ago It's very easy to re-export ESM (import) as CJS (require) and vice-versa. The main issue is that ESM by defaultFor 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.
rattray|4 years ago How do you do both? arcatek|4 years ago 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... load replies (1) inglor|4 years ago It's very easy to re-export ESM (import) as CJS (require) and vice-versa. The main issue is that ESM by defaultFor 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|4 years ago 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... load replies (1)
inglor|4 years ago It's very easy to re-export ESM (import) as CJS (require) and vice-versa. The main issue is that ESM by defaultFor 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.
rattray|4 years ago
arcatek|4 years ago
https://github.com/arcanis/clipanion/blob/master/rollup.conf...
inglor|4 years ago
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.