Running TS — tsx / ts-node
For "I want to run a TypeScript file like it's JavaScript" you need a runner. The modern choice is tsx — fast, zero config, and based on esbuild.
Install & use
SHELL
pnpm add -D tsx tsx src/index.ts # run once tsx watch src/index.ts # restart on change node --import tsx src/index.ts # via Node's --import flag
Use in scripts
package.json
{
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "tsx src/index.ts",
"seed": "tsx scripts/seed.ts"
}
}
Other runners
| Tool | Notes |
|---|---|
| tsx | Modern default. esbuild-based. Fast. |
| ts-node | Older, still works. tsx is faster and simpler. |
| Bun | Runs .ts natively. Use if Bun is your runtime. |
| Deno | Native TS support. Run with deno run script.ts. |
| Node 22+ | --experimental-strip-types runs TS without a runner. |
Node 22's native TS support
SHELL
node --experimental-strip-types script.ts
Strips type annotations and runs the result. No transformation of enums, decorators, or generics — those need a real compiler. Still useful for simple scripts.
Pitfalls
- tsx runs the file without type-checking. Keep
tsc --noEmitin CI to catch errors. - Some ESM features (top-level await, JSON imports) need flags depending on Node version.
- Globals from
@types/nodeare visible if installed — they don't run, they just type-check.
Tip: For dev:
tsx watch. For type safety: a separate npm run type-check step. Together you get fast feedback and compile-time correctness.Example
Example
// 2020s: use 'tsx' — fast and zero-config
// pnpm add -D tsx
// tsx src/index.ts
//
// ts-node still works but tsx (esbuild under the hood) is faster.
// Node 22+ also runs *.ts files natively with --experimental-strip-types.
console.log('tsx = node + TypeScript = instant');
Try it Yourself »
Exercise
Modern fast TypeScript runner.
npx
src/index.ts
Three letters.
Discussion
Loading…