Skip to content

Babel plugin and helper functions for interoperation between Node.js native ESM and Babel ESM

License

Notifications You must be signed in to change notification settings

qnighy/node-cjs-interop

Repository files navigation

babel-plugin-node-cjs-interop, swc-plugin-node-cjs-interop and node-cjs-interop: fix the default import interoperability issue in Node.js

The problem to solve

Consider the following modules:

// a.js

export default function greet() {
  console.log("Hello, world!");
}
// b.js

import greet from "a.js";

greet();

They usually work, unless the following conditions are met:

  • a.js (the module being imported) is a simulated ESM. That is, the module is transpiled as a CommonJS module (by Babel or TypeScript) before execution. And,
  • b.js (the importing module) is a native ESM, That is, the module is run on Node.js' native ES Module support.

You can reproduce the above condition by placing the following files:

// a.cjs

"use strict";

Object.defineProperty