tsx for TypeScript: Complete Guide with Examples

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
\n

tsx for TypeScript: Complete Guide with Examples

\n\n
\n

1. What It Is and Why It's Needed

\n

tsx is a modern, high-performance runner for TypeScript and ESM (ECMAScript Modules) that allows you to run .ts, .tsx, and .mts files directly, without prior compilation to JavaScript. Built on top of esbuild (an ultra-fast bundler in Go), tsx provides near-instant execution of TypeScript code, making it an ideal tool for development, prototyping, scripting, and testing.

\n\n

Why is tsx needed? Traditionally, running TypeScript files requires a two-step process: first compile the code using tsc (TypeScript Compiler), then execute the resulting JavaScript via Node.js. This slows down development iterations. tsx solves this problem by combining compilation and execution into a single step. It automatically handles TypeScript syntax, ESM modules, JSX, and even supports node_modules resolution. Essentially, tsx is a replacement for ts-node, but significantly faster and more modern.

\n\n

Key advantages of tsx over alternatives: speed (uses esbuild, which is 10-100 times faster than tsc), built-in ESM support (no need to configure tsconfig.json for modules), automatic TypeScript configuration detection, and built-in --watch mode support. tsx is perfect for: writing CLI utilities in TypeScript, running tests (Jest, Vitest), creating automation scripts, rapid prototyping, and learning TypeScript.

\n
\n\n
\n

2. Installation

\n

tsx is distributed via npm (Node Package Manager) and can be installed globally or locally in a project. Local installation is recommended for project dependency isolation.

\n\n

Local installation (recommended):

\n
npm install --save-dev tsx
\n

Or using yarn:

\n
yarn add --dev tsx
\n

Or pnpm:

\n
pnpm add --save-dev tsx
\n\n

Global installation (for quick access from anywhere):

\n
npm install -g tsx
\n\n

Verify installation:

\n
npx tsx --version
\n

After installation, you can run TypeScript files with the command npx tsx filename.ts (for local installation) or tsx filename.ts (for global installation).

\n\n

Important: tsx requires Node.js version 18 or higher. To work with ESM, ensure your package.json has the "type": "module" field or use the .mts extension for files.

\n
\n\n
\n

3. Quick Start — Minimal Working Example

\n

Create a file hello.ts with the following content:

\n
// hello.ts\nconst greet = (name: string): string => {\n    return `Hello, ${name}! Welcome to the world of tsx!`;\n};\n\nconst userName: string = "PythonLib";\nconsole.log(greet(userName));\n\n// Using modern JS features\nconst numbers: number[] = [1, 2, 3, 4, 5];\nconst doubled = numbers.map((n) => n * 2);\nconsole.log("Doubled numbers:", doubled);\n\n// Async code\nconst fetchData = async (): Promise => {\n    const data = await Promise.resolve({ id: 1, name: "Example" });\n    console.log("Async data:", data);\n};\n\nfetchData();
\n\n

Run the file with the command:

\n
npx tsx hello.ts
\n\n

Expected output:

\n
Hello, PythonLib! Welcome to the world of tsx!\nDoubled numbers: [ 2, 4, 6, 8, 10 ]\nAsync data: { id: 1, name: 'Example' }
\n\n

This example demonstrates the basic operation of tsx: it

Recommendations