babel-compat-loader
A node loader to import
modules compiled by Babel a if they were native ESM.
Installation
npm install --save babel-compat-loader
or
yarn add babel-compat-loader
Usage
node --experimental-modules --loader babel-compat-loader ./your-file.mjs
What is this loader needed for?
ECMAScript modules transpiled by Babel have a "problem": they are not ECMAScript modules anymore. They become CommonJS modules, but this creates an interoperability problem with ECMAScript modules in NodeJS.
Consider this exaple:
import { val } from "./dependency";
console.log(val);
export const val = 3;
When this program is run using node --experimental-modules index.mjs
it logs 3
, as expected.
When both the files are transpiled by Babel it still logs 3
, because Babel knows how to handle named exports defined in transpiled modules:
var _dependency = require("./dependency");
console.log(_dependency.val);
Object.defineProperty(exports, "__esModule", { value: true });
var val = exports.val = 2;
When only the dependency is transpiled by Babel the program breaks:
import { val } from "./dependency";
console.log(val);
Object.defineProperty(exports, "__esModule", { value: true });
var val = exports.val = 2;
import { val } from "./dependency";
^^^
SyntaxError: The requested module './dependency' does not provide an export named 'val'
at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)
The problem is that, since JavaScript engines need to know which variables are exported by a module before executing it, CommonJS modules only export a default value which represents the module.exports
object.
This loader fixes this problem, by analyzing the imported CommonJS modules before executing them.