Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

esbuild-esm-loader

aduth1.8kMIT0.3.3

ESM loader to transform imports using ESBuild

esm, loader, esm-loader

readme

ESBuild ESM Loader

ESBuild ESM Loader is a custom resolver implementation for the module customization hooks feature of Node.js ECMAScript modules.

Using this loader will enhance the default loader behavior to allow you to transform imported files using ESBuild, allowing for transparent imports of modern JavaScript, JSX, and TypeScript.

// index.tsx
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';

console.log(renderToStaticMarkup(<div>Hello World!</div>));
node --import=esbuild-esm-loader/register ./index.tsx

Installation

The current version of the library requires Node.js v16.17 or newer.

Install as a dependency using npm. You will also need to install ESBuild, as it's a peer dependency of this project.

npm install esbuild esbuild-esm-loader

Usage

Loaders can be defined as an argument when running node.

node --import=esbuild-esm-loader/register entry.js

For better error stack traces, this loader supports Node.js's native sourcemap support, which can be enabled by passing the --enable-source-maps flag.

node --enable-source-maps --import=esbuild-esm-loader/register entry.js

Configuration

If present, a tsconfig.json will be read and provided to ESBuild as the tsconfigRaw option. You can use this to control behavior such as JSX pragma with TypeScript's jsxFactory configuration option. This applies even if you are not using TypeScript.

Versioning

This project follows Semantic Versioning.

To better align with the experimental status of the loader hooks, initial releases will follow major version zero until the feature stabilizes in Node.js. Minor versions on the zero major will always include breaking changes. Patch versions on the zero major will include bug fixes and backwards-compatible changes.

Versioning will proceed from 1.0.0 once the feature stabilizes in Node.js.

License

Copyright 2025 Andrew Duthie

Released under the MIT License. See LICENSE.md.

changelog

v0.3.3 (2025-01-02)

New Features

v0.3.2 (2024-06-22)

Bug Fixes

  • Fix handling import specifiers including query parameters.
  • Avoid trying to process data: or node: import specifiers.

v0.3.1 (2024-02-06)

New Features

  • Add support for --import usage as alternative to deprecated --loader flag, available in Node.js v20.6.0 and newer.
    • Before: node --loader=esbuild-esm-loader ./index.jsx
    • After: node --import=esbuild-esm-loader/register ./index.jsx

Bug Fixes

  • Fix error "The URL must be of scheme file" in newer versions of Node.js.

v0.3.0 (2023-02-11)

Breaking Changes

  • Update for Node v16.17 API changes, resolving error for missing shortCircuit.

v0.2.3 (2021-11-17)

Bug Fixes

  • Fixed an issue where file names with multiple dots (e.g. example.config.js) would be resolved incorrectly.

Documentation

  • Added "Configuration" section to the documentation, explaining configurability.

v0.2.2 (2021-11-16)

Enhancements

  • The required Node.js version of >=16.12 is now enforced by package.json engines field.

Bug Fixes

  • Improved detection of module resolution for non-bare imports (e.g. root-relative paths, or file: paths)

Documentation

  • The required Node.js version of >=16.12 is now documented in the README.

v0.2.1 (2021-11-11)

Bug Fixes

  • TypeScript configuration files are now properly considered in transformation.

v0.2.0 (2021-11-08)

Breaking Changes

  • Update for Node v16.12 API changes (load replacing getFormat, transformSource)

v0.1.1 (2021-10-03)

Enhancements

  • Add support for extensionless file resolution

v0.1.0 (2021-03-13)

  • Initial release