Running with ts-node
What you'll learn: How to run TypeScript files instantly without the manual compile step you learned with tsc.
The Two-Step Problem
In the previous lesson, you learned that running TypeScript requires two steps: first compile with tsc, then execute the resulting .js file with Node. This works, but it's tedious during development—especially when you're making frequent changes and want quick feedback.
What ts-node Does
ts-node is a tool that combines both steps into one. It compiles your TypeScript code in memory and immediately executes it, without creating any .js files on your disk. Think of it like an interpreter that understands TypeScript directly.
Analogy: Using tsc is like translating a letter into another language, printing it, then reading it aloud. Using ts-node is like having a live translator who speaks both languages—they just translate and speak simultaneously.
How It Works
After installing ts-node (npm install -g ts-node or locally in your project), you simply run:
ts-node myfile.ts
Behind the scenes, ts-node:
- Reads your
tsconfig.jsonsettings - Compiles the TypeScript to JavaScript in memory
- Executes it immediately with Node
No intermediate files clutter your project directory. This makes ts-node perfect for development, prototyping, and running scripts—though for production, you'd typically still use tsc to create optimized .js files.
Key Takeaway: ts-node lets you run TypeScript files directly without manually compiling them first, keeping your workflow fast and your directory clean during development.