Skip to content

tsx

Build & PackagingBuild/Dependency ManagementTypeScript

What it is

tsx runs TypeScript files directly with no build step, using esbuild for near-instant transpilation. It is the modern replacement for ts-node.

Run or watch TypeScript entry points directly. tsx strips types without checking them, so pair it with tsc --noEmit for verification.

Installation

npm install -D tsx

Getting started

The smallest useful thing you can do with it, and what each part means.

Running and watching
# Run a script once
npx tsx scripts/seed.ts

# Restart on change during development
npx tsx watch src/server.ts

# In package.json
{
  "scripts": {
    "dev": "tsx watch src/server.ts",
    "typecheck": "tsc --noEmit"
  }
}
The separate typecheck script matters: tsx deletes types rather than checking them, so a type error will run happily until tsc catches it.

Advanced usage

Where the library earns its place over a simpler alternative.

Path aliases and ESM
// tsx honours the paths in tsconfig.json automatically
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": { "@/*": ["src/*"] },
    "module": "ESNext",
    "moduleResolution": "bundler"
  }
}

// Both of these work without extra flags:
import { config } from '@/config';
const { readFile } = require('node:fs/promises');
Mixing CommonJS and ESM is the situation that made ts-node painful. tsx resolves both, which removes most of the configuration people used to need.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Type errors do not appear
By design. Run tsc --noEmit separately; tsx only strips types.
Cannot find module '@/thing'
The path alias is missing from tsconfig, or baseUrl is not set. tsx reads them from there.

Best practices

  • Always run tsc --noEmit in CI — tsx does not typecheck.
  • Use tsx watch in development instead of nodemon plus a compile step.
  • For production, compile ahead of time or use a bundler; tsx is a development tool.
  • Prefer tsx over ts-node for new projects unless you specifically need ts-node's type checking.

Background

Why it exists, and what it was reacting to.

ts-node was slow and its ESM support was a persistent source of frustration. tsx wraps esbuild instead, starting in milliseconds and handling both module systems without configuration.