0
0
Typescriptprogramming~10 mins

Running TypeScript with ts-node - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Running TypeScript with ts-node
Write TypeScript file (example.ts)
Run command: ts-node example.ts
ts-node compiles TypeScript to JavaScript in memory
Execute compiled JavaScript immediately
Output shown in terminal
End
This flow shows how ts-node runs a TypeScript file by compiling it on the fly and then executing it immediately.
Execution Sample
Typescript
console.log('Hello from TypeScript!');
This code prints a greeting message to the terminal when run with ts-node.
Execution Table
StepActionEvaluationResult
1ts-node reads example.tsReads TypeScript codeCode loaded in memory
2ts-node compiles TypeScriptConverts to JavaScriptJavaScript code ready
3ts-node executes JavaScriptRuns console.logPrints 'Hello from TypeScript!'
4Program endsNo more codeProcess exits
💡 Program ends after executing all code in example.ts
Variable Tracker
VariableStartAfter ExecutionFinal
console.log outputnone'Hello from TypeScript!''Hello from TypeScript!'
Key Moments - 2 Insights
Why don't we see a separate JavaScript file after running ts-node?
ts-node compiles TypeScript in memory without creating a JavaScript file on disk, as shown in execution_table step 2.
Does ts-node run the TypeScript code directly?
No, ts-node first compiles TypeScript to JavaScript in memory, then runs the JavaScript immediately (execution_table steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3?
Ats-node executes the compiled JavaScript
Bts-node compiles TypeScript to JavaScript
Cts-node reads the TypeScript file
DProgram ends
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table
At which step does ts-node read the TypeScript file?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in the execution_table for reading the file
If the TypeScript code had a syntax error, at which step would ts-node fail?
AStep 3 - executing JavaScript
BStep 1 - reading file
CStep 2 - compiling TypeScript
DStep 4 - program ends
💡 Hint
Compilation errors happen during the conversion from TypeScript to JavaScript (step 2)
Concept Snapshot
Running TypeScript with ts-node:
- Use 'ts-node filename.ts' to run TS files directly
- ts-node compiles TS to JS in memory
- Then executes JS immediately
- No separate JS file is created
- Output appears in terminal right away
Full Transcript
Running TypeScript with ts-node means you can run TypeScript files directly without manually compiling them first. When you run 'ts-node example.ts', ts-node reads your TypeScript code, compiles it to JavaScript in memory, and then runs the JavaScript immediately. This process happens quickly and you see the output in your terminal. There is no separate JavaScript file saved on your disk. If there is a syntax error in your TypeScript code, ts-node will show an error during the compilation step before running anything.