babel-register-esm

raw JSON →
1.2.5 verified Sat Apr 25 auth: no javascript

An ESM loader for Babel that transpiles files on-the-fly based on Babel configuration, similar to @babel/register but for ES modules. Current version 1.2.5 is stable with moderate release cadence. It supports importing .ts and .tsx files by resolving .js imports to TypeScript files, a unique feature for TypeScript ESM projects. Requires @babel/core as a peer dependency. Actively maintained, with TypeScript type definitions shipped.

error Error [ERR_MODULE_NOT_FOUND]: Cannot find module
cause Importing a .ts file with .js extension but the loader fails to resolve it because Babel plugin for TypeScript is not configured.
fix
Ensure babelrc.json includes @babel/plugin-transform-typescript plugin and isTSX option if needed.
gotcha Must run Node.js with --loader babel-register-esm flag. Running without it will not transpile.
fix node --loader babel-register-esm your-file.js
gotcha Peer dependency @babel/core must be installed separately. If missing, loader will fail.
fix npm install @babel/core
gotcha TypeScript imports must use .js extension even when importing .ts files. Using .ts extension will fail.
fix import './file.js' instead of './file.ts'
npm install babel-register-esm
yarn add babel-register-esm
pnpm add babel-register-esm

Shows how to install, configure, and run a JSX file with on-the-fly transpilation using the ESM loader.

// Install: npm install babel-register-esm @babel/core
// Create babelrc.json with plugins, e.g.:
// {
//   "plugins": ["@babel/plugin-transform-react-jsx"]
// }

// a-file-with-jsx.js
import React from 'react';
console.log((<button>Hi</button>).type);

// Run with:
// node --loader babel-register-esm a-file-with-jsx.js

// For Mocha:
// mocha --loader=babel-register-esm some-test.js