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:
node-v
Verify TypeScript:
tsc-v
If TypeScript is installed locally in a project, use:
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:
letmessage:string="Hello from TypeScript";
console.log(message);
Option A: Compile a single file
If TypeScript is installed globally:
tsc index.ts
If TypeScript is installed locally:
npx tsc index.ts
Now run the output:
node index.js
Option B: Compile an entire project
In a project folder, compile all TypeScript files:
npx tsc
Then run the output file you want with Node, for example:
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:
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:
CSS
npm install--save-dev ts-node
Run your file:
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:
npx tsc--init
Examples you can copy
Example 1: Quick math script
functionadd(a:number,b:number):number {
returna+b;
}
console.log(add(2,3));
Run via compile + Node:
tsc index.ts
node index.js
Or run directly:
npx ts-node index.ts
Example 2: Read a command-line argument
constname=process.argv[2]??"friend";
console.log(`Hi,${name}!`);
Compile and run:
tsc index.ts
node index.js Sam
Or with ts-node:
npx ts-node index.ts Sam
Example 3: Multiple files with imports
math.ts:
CSS
exportfunctionmultiply(a:number,b:number):number {
returna*b;
}
index.ts:
JavaScript
import {multiply }from"./math";
console.log(multiply(4,5));
Compile and run:
npx tsc
node index.js
If imports start getting messy, create a project config:
npx tsc--init
Common mistakes and how to fix them
Mistake 1: Running .ts files with Node
What someone might do:
node index.ts
Why it breaks:
Node does not execute TypeScript.
Correct approach:
tsc index.ts
node index.js
Or run directly:
npx ts-node index.ts
Mistake 2: Forgetting to recompile after changes
What someone might do:
Edit index.ts
Then run:
node index.js
Why it breaks:
Node runs the old compiled file.
Correct approach:
tsc index.ts
node index.js
Or switch to:
npx ts-node index.ts
Mistake 3: Using non-relative imports for local files
What someone might do:
JavaScript
import {multiply }from"math";
Why it breaks:
That looks for a package, not a local file.
Correct approach:
JavaScript
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:
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