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.

5 min read

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:

bashbash
npm install -g typescript
tsc --version

The 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:

typescripttypescript
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:

bashbash
tsc hello.ts
node hello.js

The 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:

texttext
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:

typescripttypescript
function greet(name: string): string {
  return `Hello, ${name}!`;
}
 
console.log(greet(42));  // Error: number is not string

Compiling 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:

texttext
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:

bashbash
tsc --noEmitOnError hello.ts

Method 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:

bashbash
npm install -g tsx
tsx hello.ts

tsx 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:

texttext
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:

bashbash
tsx --watch hello.ts

Method 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:

bashbash
npm install -g ts-node
ts-node hello.ts

The 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:

texttext
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

MethodSpeedSetupBest for
tsc + nodeModerateNoneProduction builds, understanding compilation
tsxFastnpm install -g tsxDevelopment, scripts, quick experiments
ts-nodeSlowernpm install -g ts-nodeProjects 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:

jsonjson
{
  "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/:

bashbash
tsc              # Compiles everything in src/ to dist/
node dist/app.js  # Runs the compiled output

If 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:

bashbash
tsx src/app.ts   # Compiles and runs directly

Compiler 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:

bashbash
tsc --watch

The 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:

bashbash
# Terminal 1: watch and compile
tsc --watch
 
# Terminal 2: watch and run
nodemon dist/app.js

Most 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

Rune AI

Key Insights

  • Install TypeScript globally with npm install -g typescript.
  • Compile .ts files to .js with tsc filename.ts, then run with node filename.js.
  • Use tsx filename.ts for the fastest TypeScript execution during development.
  • ts-node is an alternative for direct execution, but tsx is faster for new projects.
  • Type annotations are always stripped. The runtime only sees JavaScript.
RunePowered by Rune AI

Frequently Asked Questions

Can I run .ts files directly with Node.js?

Yes, as of Node.js 24 LTS, type stripping is enabled by default for .ts files that use only erasable syntax, so no flag is needed. Features that need runtime code generation, such as enums and decorators, still require `tsx`, `ts-node`, or compiling with `tsc` first.

What is the difference between tsx and ts-node?

tsx is faster and simpler. It uses esbuild under the hood and starts almost instantly. ts-node is older and supports more TypeScript features by default but is slower to start. For most projects, use tsx.

Do I need a tsconfig.json file to run TypeScript?

For `tsx`, no. It works without any configuration. For `tsc`, having a tsconfig.json gives you control over compiler options, but a minimal setup works without one.

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.