Node.js enhanced with esbuild to run TypeScript & ESM
- Blazing fast on-demand TypeScript & ESM compilation
- Works in both CommonJS and ESM packages
- Supports next-gen TypeScript extensions (
.cts
&.mts
) - Supports
node:
import prefixes - Hides experimental feature warnings
- TypeScript REPL
- Tested on Linux & Windows with Node.js v12~18
npm install --save-dev tsx
Install it globally to use it anywhere, outside of your npm project, without npx.
npm install --global tsx
Note: Commands are prefixed with
npx
to execute thetsx
binary, but it's not necessary if globally installed or when using it in thescript
object inpackage.json
Pass in a file to run:
npx tsx ./file.ts
Run file and automatically re-run on changes.
All imported files are watched except from the following directories:
node_modules
, bower_components
, vendor
, dist
, and .*
(hidden directories).
Press Return to manually re-run.
npx tsx watch ./file.ts
Start a TypeScript REPL by running tsx
with no arguments.
npx tsx
Modules transformations are cached in the system cache directory (TMPDIR
). Transforms are cached by content hash so duplicate dependencies are not re-transformed.
Set the --no-cache
flag to disable the cache:
npx tsx --no-cache ./file.ts
-
@esbuild-kit/esm-loader - Node.js Loader to transform TypeScript to ESM.
-
@esbuild-kit/cjs-loader - Node.js
requie()
hook to transform TypeScript & ESM to CommonJS.
No, esbuild does not support type checking.
It's recommended to run TypeScript separately as a command (tsc --noEmit
) or via IDE IntelliSense.
How is tsx
different from ts-node
?
They are both tools to run TypeScript files.
The main difference is that tsx
is powered by esbuild for blazing fast TypeScript compilation. Whereas ts-node
uses the TypeScript compiler, which is not as fast.
Because esbuild doesn't do type checking, tsx
is more equivalent to ts-node --transpileOnly
.
If you migrated from ts-node
, please share your performance gains here!