How to Run TypeScript
What you’ll build or solve
You’ll run a TypeScript file from the command line using two common methods. By the end, you’ll be able to run a single file and also run TypeScript directly during development.
When this approach works best
This method works best when:
Learn TypeScript on Mimo
- You want to run TypeScript scripts locally on your machine.
- You’re building a Node.js project and need a reliable way to execute TypeScript.
- You want quick iteration while coding, without manually compiling every time.
Skip this approach if you’re working in an online editor that already runs TypeScript for you.
Prerequisites
- Node.js installed
- TypeScript installed (global or local)
Check your setup:
Bash
node -v
Verify TypeScript:
Bash
tsc -v
If TypeScript is installed locally in a project, use:
Bash
npx tsc -v
Step-by-step instructions
Step 1: Compile and run with tsc + Node.js
This is the standard way to run TypeScript. TypeScript compiles to JavaScript, then Node runs the JavaScript output.
Create index.ts:
let message: string = "Hello from TypeScript";
console.log(message);
Option A: Compile a single file
If TypeScript is installed globally:
Bash
tsc index.ts
If TypeScript is installed locally:
Bash
npx tsc index.ts
Now run the output:
Bash
node index.js
Option B: Compile an entire project
In a project folder, compile all TypeScript files:
Bash
npx tsc
Then run the output file you want with Node, for example:
Bash
node index.js
What to look for
- A new
.jsfile appears after compilation. - If
tscis not recognized,npx tscshould still work inside a project. - For multi-file projects, a
tsconfig.jsonfile helps. You can create one with:
Bash
npx tsc --init
Keep that as setup for projects, not a separate running method.
Step 2: Run directly with ts-node (alternative)
Use this method for development when you want to skip manual compilation.
Install ts-node locally:
Bash
npm install --save-dev ts-node
Run your file:
Bash
npx ts-node index.ts
What to look for
- Your code runs without creating an
index.jsfile. - If you see module-related errors, you may need a basic
tsconfig.jsoncreated with:
Bash
npx tsc --init
Examples you can copy
Example 1: Quick math script
function add(a: number, b: number): number {
return a + b;
}
console.log(add(2, 3));
Run via compile + Node:
Bash
tsc index.ts
node index.js
Or run directly:
Bash
npx ts-node index.ts
Example 2: Read a command-line argument
const name = process.argv[2] ?? "friend";
console.log(`Hi, ${name}!`);
Compile and run:
Bash
tsc index.ts
node index.js Sam
Or with ts-node:
Bash
npx ts-node index.ts Sam
Example 3: Multiple files with imports
math.ts:
export function multiply(a: number, b: number): number {
return a * b;
}
index.ts:
import { multiply } from "./math";
console.log(multiply(4, 5));
Compile and run:
Bash
npx tsc
node index.js
If imports start getting messy, create a project config:
Bash
npx tsc --init
Common mistakes and how to fix them
Mistake 1: Running .ts files with Node
What someone might do:
Bash
node index.ts
Why it breaks:
Node does not execute TypeScript.
Correct approach:
Bash
tsc index.ts
node index.js
Or run directly:
Bash
npx ts-node index.ts
Mistake 2: Forgetting to recompile after changes
What someone might do:
Edit index.ts
Then run:
Bash
node index.js
Why it breaks:
Node runs the old compiled file.
Correct approach:
Bash
tsc index.ts
node index.js
Or switch to:
Bash
npx ts-node index.ts
Mistake 3: Using non-relative imports for local files
What someone might do:
import { multiply } from "math";
Why it breaks:
That looks for a package, not a local file.
Correct approach:
import { multiply } from "./math";
Troubleshooting
If you see tsc: command not found, restart your terminal, or use npx tsc.
If npx tsc compiles but you can’t find output files, check the folder you’re running in and look for .js files next to your .ts files.
If you see permission errors during global installs, use a local install and run npx tsc instead.
If you see Cannot find module './math', confirm the import path starts with ./ and the file name matches exactly.
If you see Cannot use import statement outside a module, create a tsconfig.json with:
Bash
npx tsc --init
Then rerun your command.
Quick recap
- Method 1: compile with
tsc(ornpx tsc), then run withnode. - Method 2: run directly with
npx ts-nodeduring development. - Avoid
node index.ts, Node can only run JavaScript. - Recompile after changes, or use
ts-nodeto skip that step. - Use
npx tsc --initwhen you need project-level TypeScript settings.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot