How to Run TypeScript from the Command Line: From Simple to Professional
\n\nYou'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?
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.
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\nWhy Doesn't Node.js Understand TypeScript?
\n\nBefore rushing for tools, let's understand the essence. When you write:
\n\nconst greet = (name: string): string => {\n return `Hello, ${name}!`;\n};\nconsole.log(greet('PythonLib'));\n\nNode.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.
There are two fundamentally different approaches:
\n- \n
- Two-step: first compile with
tsc script.ts, then run withnode script.js. \n - One-step: using utilities that do both actions for you "on the fly" (JIT compilation). \n
We'll cover both options, but we'll start with the most popular one — the one-step approach.
\n\nMethod 1: ts-node — The Classic
\n\nts-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.
Installation and Execution
\n\nnpm install -g ts-node typescript @types/node\nts-node script.ts\n\nThat'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.
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\nRun it: ts-node api-demo.ts. The result appears immediately in the console.
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-nodecommand 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
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\nMethod 2: tsx — The Modern and Fast Successor
\n\ntsx (TypeScript Execute) is a newer utility,