How to Run TypeScript from the Command Line: 3 Working Methods

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Run TypeScript from the Command Line: From Simple to Professional

\n\n

You've written your first TypeScript script. It looks great: strict typing, concise syntax, autocomplete in the IDE. But when you open the terminal and type node script.ts, Node.js throws an error. Sound familiar?

\n\n

TypeScript is a superset of JavaScript, and Node.js cannot execute .ts files directly. In this article, we'll cover all the real ways to run TypeScript from the command line. You'll learn how quick execution differs from a production build, when to use ts-node and when to use tsx, and how not to get confused by the tools.

\n\n

We draw on a popular Stack Overflow question (over 500,000 views), but provide not just an answer, but a full comparison of approaches. Let's go!

\n\n

Why Doesn't Node.js Understand TypeScript?

\n\n

Before rushing for tools, let's understand the essence. When you write:

\n\n
const greet = (name: string): string => {\n  return `Hello, ${name}!`;\n};\nconsole.log(greet('PythonLib'));
\n\n

Node.js sees : string and doesn't know what to do with it. JavaScript does not support static typing as part of its syntax. Therefore, TypeScript must first be transpiled (converted) into JavaScript, and only then executed.

\n\n

There are two fundamentally different approaches:

\n
    \n
  • Two-step: first compile with tsc script.ts, then run with node script.js.
  • \n
  • One-step: using utilities that do both actions for you "on the fly" (JIT compilation).
  • \n
\n\n

We'll cover both options, but we'll start with the most popular one — the one-step approach.

\n\n

Method 1: ts-node — The Classic

\n\n

ts-node is a TypeScript execution engine for Node.js. It compiles code in memory and immediately passes it to Node.js. This is the oldest and most proven way to run TypeScript from the command line without extra effort.

\n\n

Installation and Execution

\n\n
npm install -g ts-node typescript @types/node\nts-node script.ts
\n\n

That's it. Your file will execute without creating an intermediate JS file. Under the hood, ts-node uses the standard TypeScript compiler (tsc), but does so transparently for the developer.

\n\n

Example: Working with an API

\n\n
// api-demo.ts\nimport axios from 'axios';\n\ninterface Post {\n  userId: number;\n  id: number;\n  title: string;\n}\n\nasync function fetchFirstPost(): Promise<Post> {\n  const response = await axios.get<Post>('https://jsonplaceholder.typicode.com/posts/1');\n  return response.data;\n}\n\nfetchFirstPost().then(post => console.log(post.title));\n// Output: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
\n\n

Run it: ts-node api-demo.ts. The result appears immediately in the console.

\n\n

Pros and Cons of ts-node

\n\n
    \n
  • + Simplicity: one command instead of two.
  • \n
  • + Support for tsconfig.json: all your TypeScript settings are respected.
  • \n
  • + REPL: the ts-node command without arguments opens an interactive TypeScript shell.
  • \n
  • - Speed: compilation happens on every run, which is noticeable on large projects.
  • \n
  • - Memory consumption: during long-running tasks (e.g., watch mode), it can consume RAM.
  • \n
\n\n

When to use: for scripts, prototypes, small utilities. If you need to run a TypeScript file from the command line quickly and without configuration — ts-node is your choice.

\n\n

Method 2: tsx — The Modern and Fast Successor

\n\n

tsx (TypeScript Execute) is a newer utility,

Blogs

Book Recommendations