Docs mention of setting "type" to "module" in Package.json #1429
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That enables ECMAScript modules (ESM) in Node ( I won’t go into the details of ESM and the whole history, but it’s been chosen as the standard because it’s statically-analyzable (an essential for the web so imports can be fetched after the parsing phase rather the execution phase, resulting in a drastic speedup) and allows easier tree-shaking. If you’re writing client code you should always use ESM (because that’s what the browser expects). If only Node, the choice is up to you, but more and more packages are upgrading to ESM so you’ll find compatibility for CJS fading over time (you’ll also find many complaints about this currently-fragmented ecosystem, but they are incompatible). If you switch to ESM and see more errors, it’s because most of the packages you’re depending on are older (which might be a good time to dive into what’s not needed anymore with newer Node versions and/or what’s built into modern browsers now). Anyways, though this package does ship ESM and CJS, ESM is the first-class citizen and will always work as intended, whereas CJS is merely “as-is.” And though there are no known incompatibilities with this package and CJS, you’ll still have a better experience with ESM. |
Beta Was this translation helpful? Give feedback.
That enables ECMAScript modules (ESM) in Node (
import
/export
). ESM is the new universal standard for JS and works in Node and the browser. By default, Node still uses CommonJS (CJS) (require()
) for backwards compatibility, but is being phased out. Other projects like Deno also rely on ESM.I won’t go into the details of ESM and the whole history, but it’s been chosen as the standard because it’s statically-analyzable (an essential for the web so imports can be fetched after the parsing phase rather the execution phase, resulting in a drastic speedup) and allows easier tree-shaking.
If you’re writing client code you should always use ESM (because that’s what the browser expects). If only…