babel-plugin-node-cjs-interop
, swc-plugin-node-cjs-interop
and node-cjs-interop
: fix the default import interoperability issue in Node.js
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