tsx for TypeScript: Complete Guide with Examples
\n\n1. What It Is and Why It's Needed
\ntsx 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.
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.
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.
2. Installation
\ntsx 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\nLocal installation (recommended):
\nnpm install --save-dev tsx\nOr using yarn:
\nyarn add --dev tsx\nOr pnpm:
\npnpm add --save-dev tsx\n\nGlobal installation (for quick access from anywhere):
\nnpm install -g tsx\n\nVerify installation:
\nnpx tsx --version\nAfter installation, you can run TypeScript files with the command npx tsx filename.ts (for local installation) or tsx filename.ts (for global installation).
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.
3. Quick Start — Minimal Working Example
\nCreate a file hello.ts with the following content:
// 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\nRun the file with the command:
\nnpx tsx hello.ts\n\nExpected output:
\nHello, PythonLib! Welcome to the world of tsx!\nDoubled numbers: [ 2, 4, 6, 8, 10 ]\nAsync data: { id: 1, name: 'Example' }\n\nThis example demonstrates the basic operation of tsx: it