Image for post
Image for post

webpack 4: import() and CommonJs

One of the breaking changes in webpack 4 is the behavior of import() when importing non-ESM (i. e. CommonJS modules).

Tobias Koppers
Feb 14, 2018 · 3 min read
// (A) source.js
import("./target").then(result => console.log(result));
// (B) source.mjs
import("./target").then(result => console.log(result));
// (1) target.js
exports.name = "name";
exports.default = "default";
// (2) target.js
exports.__esModule = true;
exports.name = "name";
exports.default = "default";
// (3) target.js or target.mjs
export const name = "name";
export default "default";
// (4) target.json
{ name: "name", default: "default" }
{ __esModule: true, name: "name", default: "default" }
// webpack 3
{ name: "name", default: "default" }
// webpack 4
{ name: "name", default: { name: "name", default: "default" } }
{ default: { name: "name", default: "default" } }
{ __esModule: true, name: "name", default: "default" }
{ default: { __esModule: true, name: "name", default: "default" } }
{ name: "name", default: { name: "name", default: "default" } }

module.exports = 42;
// webpack 3
42
// webpack 4
{ default: 42 }

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