tsx
_TypeScript Execute (tsx)_: Node.js enhanced to run TypeScript & ESM files
Features
- Blazing fast on-demand TypeScript & ESM compilation
- Supports next-gen TypeScript extensions (.cts & .mts)
- Hides experimental feature warnings
- TypeScript REPL
- Resolves tsconfig.json [paths](https://www.typescriptlang.org/tsconfig#paths)
💡 Protip: Looking to bundle your TypeScript project?
>
If you're looking for a dead simple way to bundle your TypeScript projects, take a look at [pkgroll](https://github.com/privatenumber/pkgroll). It's an esbuild-enhanced Rollup that's auto configured based on your package.json!
About
tsx is a CLI command (alternative to node) for seamlessly running TypeScript & ESM, in both commonjs & module package types.
It's powered by
esbuild so it's insanely fast.
Want to just run TypeScript code? Try tsx:
- ```sh
- npx tsx ./script.ts
- ```
Mission
tsx strives to:
1. Enhance Node.js with TypeScript compatibility
2. Improve ESM <-> CJS interoperability
Install
Local installation
If you're using it in an npm project, install it as a development dependency:
- ```sh
- npm install --save-dev tsx
- ```
You can reference it directly in the package.json#scripts object:
- ```json5
- {
- "scripts": {
- "dev": "tsx ..."
- }
- }
- ```
To use the binary, you can call it with [npx](https://docs.npmjs.com/cli/v8/commands/npx) while in the project directory:
Global installation
If you want to use it in any arbitrary project without [npx](https://docs.npmjs.com/cli/v8/commands/npx), install it globally:
- ```sh
- npm install --global tsx
- ```
Then, you can call tsx directly:
Usage
tsx is designed to be a drop-in replacement for node, so you can use it just the way you would use Node.js. All command-line arguments (with the exception of a few) are propagated to Node.js.
Run TypeScript / ESM / CJS module
Pass in a file to run:
Custom tsconfig.json path
By default, tsconfig.json will be detected from the current working directory.
To set a custom path, use the --tsconfig flag:
- ```sh
- tsx --tsconfig ./path/to/tsconfig.custom.json ./file.ts
- ```
Alternatively, use the TSX_TSCONFIG_PATH environment variable:
- ```sh
- TSX_TSCONFIG_PATH=./path/to/tsconfig.custom.json tsx ./file.ts
- ```
Watch mode
Run file and automatically rerun on changes:
- ```sh
- tsx watch ./file.ts
- ```
All imported files are watched except from the following directories:
node_modules, bower_components, vendor, dist, and .* (hidden directories).
Ignore files from watch
To exclude files from being watched, pass in a path or glob to the --ignore flag:
- ```sh
- tsx watch --ignore ./ignore-me.js --ignore ./ignore-me-too.js ./file.ts
- ```
Tips
- Press
Return to manually rerun
- Pass in --clear-screen=false to disable clearing the screen on rerun
REPL
Start a TypeScript REPL by running with no arguments:
Cache
Modules transformations are cached in the system cache directory ([TMPDIR](https://en.wikipedia.org/wiki/TMPDIR)). Transforms are cached by content hash, so duplicate dependencies are not re-transformed.
Set the --no-cache flag to disable the cache:
- ```sh
- tsx --no-cache ./file.ts
- ```
Alternatively, use the TSX_DISABLE_CACHE environment variable:
- ```sh
- TSX_DISABLE_CACHE=1 tsx ./file.ts
- ```
Node.js Loader
tsx is a standalone binary designed to be used in place of node, but sometimes you'll want to use node directly. For example, when adding TypeScript & ESM support to npm-installed binaries.
To use tsx as a Node.js loader, pass it in to the [--import](https://nodejs.org/api/module.html#enabling) flag. This will add TypeScript & ESM support for both Module and CommonJS contexts.
- ```sh
- node --import tsx ./file.ts
- ```
Or as an environment variable:
- ```sh
- NODE_OPTIONS='--import tsx' node ./file.ts
- ```
Note: The loader is limited to adding support for loading TypeScript/ESM files. CLI features such as _watch mode_ or suppressing "experimental feature" warnings will not be available.