esbuild-register

3.6.0 · maintenance · verified Sun Apr 19

esbuild-register is a utility that enables on-the-fly transpilation of JSX, TypeScript, and modern JavaScript (esnext) features in Node.js environments by leveraging the highly performant esbuild bundler. It functions as a Node.js `require` hook or an experimental `--loader`, allowing developers to execute TypeScript or JSX files directly without a prior build step. The current stable version is 3.6.0. While widely used, the package has a slow release cadence, with the last update two years ago, leading to it being classified as having 'not healthy' project activity by some analyses. Its primary differentiator compared to alternatives like `ts-node` or `babel-register` is its speed, attributed to `esbuild` being written in Go and optimized for fast compilation. It automatically respects `jsxFactory`, `jsxFragmentFactory`, and `target` options from `tsconfig.json`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates running a simple TypeScript HTTP server using both the `require` hook and the experimental ESM loader.

/* file.ts */
import { createServer } from 'http';

interface Greeter {
  greet(name: string): string;
}

const myGreeter: Greeter = {
  greet(name: string): string {
    return `Hello, ${name}! This is esbuild-register running TypeScript.`;
  },
};

const server = createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end(myGreeter.greet('World') + '\n');
});

const PORT = process.env.PORT ?? '3000';
server.listen(Number(PORT), () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  console.log('Try running with: node -r esbuild-register file.ts');
  console.log('Or for ESM projects: node --loader esbuild-register/loader -r esbuild-register file.ts');
});

view raw JSON →