What is esbuild-register?
The esbuild-register package allows you to transpile your code on the fly using esbuild. This is particularly useful for running TypeScript or modern JavaScript that uses JSX or other newer syntax without a separate build step. It hooks into Node.js's require in order to automatically compile files on-the-fly when they are required.
What are esbuild-register's main functionalities?
Transpile TypeScript on the fly
This feature allows you to automatically transpile TypeScript files when they are required in Node.js. This is particularly useful for development environments, where you want to avoid a separate build step for TypeScript.
require('esbuild-register/dist/node').register({
loader: 'tsx'
});
require('./your-ts-file.ts');
Transpile modern JavaScript (ES6+) on the fly
With this feature, you can automatically transpile modern JavaScript files that use ES6+ features or JSX. This is useful for running modern JavaScript directly in Node.js without pre-compilation.
require('esbuild-register').register({
loader: 'jsx'
});
require('./your-modern-js-file.js');
Other packages similar to esbuild-register
ts-node
ts-node is a TypeScript execution engine and REPL for Node.js. It directly executes TypeScript files and compiles them on the fly. Compared to esbuild-register, ts-node might be slower for some projects because it uses the TypeScript compiler, which is generally slower than esbuild.
babel-register
babel-register hooks into Node.js's require to automatically transpile files on the fly using Babel. It supports a wide range of JavaScript features and experimental syntax through plugins. While more flexible and powerful, it is typically slower than esbuild-register due to the nature of Babel's compilation process.
sucrase
Sucrase is a super-fast alternative to Babel for builds and development workflows. It focuses on compiling non-standard JavaScript syntax like JSX, TypeScript, and Flow into standard JavaScript. While similar in performance to esbuild-register for some use cases, it lacks some of esbuild's optimizations and features.
esbuild-register
Install
npm i esbuild esbuild-register -D
yarn add esbuild esbuild-register --dev
pnpm add esbuild esbuild-register -D
Usage
node -r esbuild-register file.ts
It will use jsxFactory
, jsxFragmentFactory
and target
options from your tsconfig.json
Experimental loader support
When using in a project with type: "module"
in package.json
, you need the --loader
flag to load TypeScript files:
node --loader esbuild-register/loader -r esbuild-register ./file.ts
Programmatic Usage
const { register } = require('esbuild-register/dist/node')
const { unregister } = register({
})
unregister()
License
MIT © EGOIST