Skip to content

Instantly share code, notes, and snippets.

@suntong
Last active June 11, 2022 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suntong/495573d515a086198154597968b75ee2 to your computer and use it in GitHub Desktop.
Save suntong/495573d515a086198154597968b75ee2 to your computer and use it in GitHub Desktop.
Get Ready For ESM

Get Ready For ESM

Note,

The following was first published by Sindre Sorhus in Jan 13 2021 at https://blog.sindresorhus.com/get-ready-for-esm-aa53530b3f77 but have since disappeard from the Internet. Thus republishing it below --

JavaScript Modules will soon be a reality for Node.js package maintainers.

This blog post is targeted Node.js package maintainers.

At the end of April 2021, Node.js 10 will be end-of-life, which means that package maintainers can target Node.js 12. This Node.js version has full support for JavaScript Modules, also known as ESM.

Image for post

From the left: Addy Osmani, Dave Herman, and Sindre Sorhus.

Background

Personally, I have been waiting for ESM to happen for almost 10 years. In 2012, I was sitting at a conference dinner with Addy Osmani, and Dave Herman was asking for feedback on early ESM syntax.

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.

If you're not familiar with ESM, start by reading the MDN overview. The Node.js docs on ESM are also very comprehensive, and IMHO required reading.

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.

Migrating

There are two ways to move your packages to ESM:

  1. 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.
  2. Dual --- ESM with a build step that transpiles a CommonJS fallback
    This requires you to also set up a build step and add a exports field 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.

The Plan

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.

FAQ

What's the benefit of ESM over CommonJS for server-only packages?

note: originally blank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment