This blog post is targeted Node.js package maintainers.
CommonJS has served us well for many years, but ESM comes with many benefits, like language-level syntax, browser support, defaults to strict mode, async loading, top-level await, improved static analysis & tree-shaking, and more.
Introducing a new module system is not a simple thing. The ecosystem is huge and it’s going to take many years to move the whole ecosystem. Luckily, smart people have spent a lot of effort into making these module systems fairly compatible. ESM can import CommonJS and CommonJS can import ESM. However, and it’s a big *however*, CommonJS can only import ESM asynchronously.
There are two ways to move your packages to ESM:
- Pure ESM
This has the benefit that it’s easier to set up. You just add
"type": "module"to your package.json, require Node.js 12, update docs & code examples, and do a major release.
- Dual — ESM with a build step that transpiles a CommonJS fallback
This requires you to also set up a build step and add a
exportsfield to your package.json. Read more
Personally, I plan to do
1 as I think it’s better to rip off the bandaid and push the ecosystem forward.
I intend to move most of my 1K+ packages to pure ESM within 2021. I’m hoping other package maintainers will follow.
If you’re a Node.js package maintainer, please consider setting aside some time in May to move your packages to ESM. It will benefit the whole ecosystem!
You can read more about my ESM plan here.
If you have any questions about moving to ESM, you can ask here.