Thoughts on ES Modules in Node.js

2019-05-10 20:27 +09:00

With its latest release (12), Node.js finally starts to support ES Modules. I’ve tested it out with my personal project, Mitty, and here are my (short) thoughts.

The best thing of the support is that we can use the same code in browsers and Node.js with no dependency or build tools. It actually works very well in Mitty’s demo page. I even used a 3rd-party module loaded with HTTPS (thanks to UNPKG).

import { beautify } from "[email protected]/dist/index.mjs";

However, there are still a few drawbacks.

  • --experimental-modules and --no-warnings are needed everytime.
  • It’s really hard to make external packages work in both browsers and Node.js.
    • Browsers don’t allow bare specifiers
    • Node.js doesn’t resolve packages from HTTPS (Resolve hook?)
    • There are actually few npm packages supporting ES Modules. Most of them use UMD.
  • It is not compatible with old systems. Babel-ing is still the safe way.

Anyway, it’s really funny that first we got nothing , then CommonJS on Node.js, then browserify (and so on) to run CommonJS on browsers, and then ES Modules on browsers, and finally ES Modules on Node.js again.

Still a long way to go, but I’m already excited for the future.

read other posts