Run TypeScript Code from the Command Line
Learn the three ways to run TypeScript from the terminal: compile with tsc and run with Node.js, run directly with tsx, or use ts-node for one-shot execution.
You run TypeScript from the command line by converting it to JavaScript first, then letting Node.js execute the result. There are three main ways to do this, from the manual two-step compile-and-run to instant execution with modern tools.
The fastest way to get started is to install TypeScript globally and write a small file.
Install TypeScript
Open your terminal and install TypeScript globally with npm, which is included with Node.js. Then check that the compiler is on your path:
npm install -g typescript
tsc --versionThe install command downloads the TypeScript compiler so tsc works from any project on your machine. The version check should print something like Version 7.0. If you do not have Node.js installed at all, download it from nodejs.org first.
Method 1: Compile with tsc, Run with Node
This is the classic approach: write a .ts file, compile it to .js with the TypeScript compiler, then run the plain JavaScript with Node. Create a file called hello.ts with this content:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("TypeScript"));This function takes a name and returns a greeting string. Save the file, then compile and run it with two commands:
tsc hello.ts
node hello.jsThe first command reads hello.ts and writes hello.js in the same folder. The second command runs that JavaScript file with Node and prints the result:
Hello, TypeScript!If your code has a type error, the compiler still reports it even though it produces JavaScript by default. Here is hello.ts with an intentional mistake, passing a number where a string is expected:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet(42)); // Error: number is not stringCompiling this file does not stop on the error by default. It still writes hello.js, but it also prints a diagnostic to the terminal describing exactly what went wrong:
hello.ts:5:20 - error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.The message gives you the file, line, column, error code, and a plain description, so you can jump straight to the problem, fix the argument, and recompile. To stop the compiler from writing JavaScript at all when there is an error, add the --noEmitOnError flag:
tsc --noEmitOnError hello.tsMethod 2: Run Directly with tsx
tsx is a modern tool that compiles and runs TypeScript in a single step, and it is the fastest way to run TypeScript during development. Install it globally, then run the same hello.ts file directly, with no separate compile step:
npm install -g tsx
tsx hello.tstsx prints the exact same result as the compile-and-run approach above, but this time no intermediate .js file is ever written to disk anywhere in the project:
Hello, TypeScript!tsx compiles the file in memory and hands the result straight to Node.js, which makes it ideal for scripts, development servers, and quick experiments. It can also watch your files and re-run automatically on every save, which is useful while you are actively editing a small script:
tsx --watch hello.tsMethod 3: Use ts-node
ts-node is the older tool for direct TypeScript execution. It uses the full TypeScript compiler under the hood, so it starts slower than tsx, but it supports every TypeScript feature out of the box. Install it and run the same file:
npm install -g ts-node
ts-node hello.tsThe output is identical to the other two methods, because all three approaches ultimately run the exact same compiled JavaScript underneath, just with a different path to get there:
Hello, TypeScript!ts-node reads your tsconfig.json automatically if one exists in the project, which makes it a reasonable choice for projects that already have complex compiler configurations in place.
Which Method Should You Use
| Method | Speed | Setup | Best for |
|---|---|---|---|
tsc + node | Moderate | None | Production builds, understanding compilation |
tsx | Fast | npm install -g tsx | Development, scripts, quick experiments |
ts-node | Slower | npm install -g ts-node | Projects with complex tsconfig settings |
For learning TypeScript, start with tsc + node so you understand the compile step. Once you are comfortable, switch to tsx for faster feedback. For details on project setup, see TypeScript project setup for beginners.
Working with Multiple Files
When your project grows beyond a single file, you need a tsconfig.json to tell the compiler how to handle the project. Here is a minimal one:
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}With this config in your project root, running tsc with no arguments compiles every .ts file in src/ and outputs the JavaScript to dist/:
tsc # Compiles everything in src/ to dist/
node dist/app.js # Runs the compiled outputIf your project uses tsx instead, you can skip the compile step entirely. Point it at the entry file and it compiles and runs everything it imports on the fly:
tsx src/app.ts # Compiles and runs directlyCompiler Watch Mode
During development, you can tell tsc to watch your files in the background and automatically recompile every time you save a change, instead of running the command by hand each time:
tsc --watchThe compiler stays running and recompiles whenever you save a .ts file. Combine this with a Node.js watcher like nodemon for a fast development loop:
# Terminal 1: watch and compile
tsc --watch
# Terminal 2: watch and run
nodemon dist/app.jsMost projects use tsx for development instead, which handles both compilation and running in a single command.
The next step is to set up your editor for the best TypeScript experience. See set up TypeScript in VS Code.
Rune AI
Key Insights
- Install TypeScript globally with
npm install -g typescript. - Compile .ts files to .js with
tsc filename.ts, then run withnode filename.js. - Use
tsx filename.tsfor the fastest TypeScript execution during development. ts-nodeis an alternative for direct execution, but tsx is faster for new projects.- Type annotations are always stripped. The runtime only sees JavaScript.
Frequently Asked Questions
Can I run .ts files directly with Node.js?
What is the difference between tsx and ts-node?
Do I need a tsconfig.json file to run TypeScript?
Conclusion
You have three reliable ways to run TypeScript from the terminal. Use tsx for the fastest feedback during development. Use tsc followed by Node.js for production builds where you want to ship plain JavaScript. Use ts-node if your project already depends on it. All three produce the same runtime behavior because TypeScript always compiles to plain JavaScript before execution.
More in this topic
TypeScript Compiler Explained
The TypeScript compiler (tsc) turns .ts files into .js files while checking types. Learn what it does, how to use it, and how it fits your workflow.
Set Up ESLint for TypeScript
Install and configure ESLint with typescript-eslint to catch bugs and enforce consistent code style in your TypeScript project.
Pick Utility Type in TypeScript
Pick creates a new type by selecting only the properties you name from an existing type. Use it to build focused types for specific views, API responses, or function parameters.