ts-node & tsx
Compiling with tsc and then running node dist/index.js is correct for production but tedious in development — you don't want a manual build on every save. ts-node and tsx solve this by running TypeScript files directly, compiling on the fly, with a watch mode that restarts on change. They make the TS dev loop as fast as plain JavaScript. This page covers what each tool does, the key difference (type-checking vs pure transpilation), tsx's speed, watch mode, and the important rule: these are development tools — production still runs compiled JavaScript.
Running TypeScript directly
# tsx — fast, zero-config, the modern favorite: npm install --save-dev tsx npx tsx src/index.ts # runs it directly — no build step npx tsx watch src/index.ts # + auto-restart on file change # ts-node — the established option: npm install --save-dev ts-node typescript npx ts-node src/index.ts
The key difference — type-checking vs transpiling
|
|
| |
|---|---|---|---|
Speed | Very fast | Slower | Fast |
Type-checks while running? | No | Yes | No |
Catches type errors at runtime | No — it just strips types | Yes — refuses to run on errors | No |
Setup | Zero-config | Reads | Reads |
Watch mode — the fast dev loop
package.json
{
"scripts": {
"dev": "tsx watch src/index.ts", // restarts the process on any source change
"build": "tsc", // production build → dist/
"start": "node dist/index.js", // production run (compiled JS)
"typecheck": "tsc --noEmit" // the type-checking gate (since tsx skips it)
}
}