The state of JavaScript modules

Johannes Ewald
May 23, 2017 · 10 min read
Image for post
Image for post
ESM, CJS, UMD, AMD — which one should I pick?

An ancient fear

Current implementations

ESM in browsers

<script type="module" src="main.js"></script>
// Supported:
import {foo} from 'https://jakearchibald.com/utils/bar.js';
import {foo} from '/utils/bar.js';
import {foo} from './bar.js';
import {foo} from '../bar.js';

// Not supported:
import {foo} from 'bar.js';
import {foo} from 'utils/bar.js';
// Example from https://jakearchibald.com/2017/es-modules-in-browsers/

ESM with webpack

Simplified webpack output which simulates the behavior of ES modules
console.log(this);
“use strict”;console.log(undefined);
(function(module, exports) {

console.log(this);

})

ESM in Node.js

Simplified function wrapper around CommonJS modules in Node.js
// CJS
const { a, b } = require("c");
// ESM
import { a, b } from "c";

Will *.mjs be the Python 3 of Node.js?

Image for post
Image for post
Python 3 is killing Python

What does this all mean for me?

Image for post
Image for post
Look at all these tree shaken modules!

Summary

webpack

The official Medium publication for the webpack open source…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store