Configs2026年5月18日·1 分钟阅读

tsx — The Fastest Way to Run TypeScript in Node.js

tsx (TypeScript Execute) is a lightweight CLI that runs TypeScript and ESM files in Node.js with near-instant startup. It uses esbuild under the hood for fast transpilation without requiring tsconfig or a build step.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
tsx TypeScript Execute
通用 CLI 安装命令
npx tokrepo install e388a8b5-52b5-11f1-9bc6-00163e2b0d79

Introduction

tsx makes running TypeScript on Node.js as simple as running JavaScript. It patches Node's module resolution to transpile TypeScript on the fly using esbuild, giving you sub-second startup times with zero configuration. It is designed as a modern, faster replacement for ts-node in everyday development workflows.

What tsx Does

  • Runs TypeScript, JSX, and TSX files directly on Node.js without a build step
  • Provides a watch mode that restarts on file changes via tsx watch
  • Handles both ESM and CommonJS modules transparently
  • Supports Node.js command-line flags like --inspect for debugging
  • Works as a drop-in replacement for node in scripts and npm scripts

Architecture Overview

tsx registers Node.js module hooks that intercept TypeScript file loads and pipe them through esbuild's transform API. Because esbuild is written in Go and runs natively, transpilation completes in single-digit milliseconds even for large files. tsx does not perform type checking, keeping the feedback loop fast. For ESM support, it uses Node's loader API to handle .ts and .tsx extensions natively.

Self-Hosting & Configuration

  • Install with npm install -D tsx as a dev dependency or globally with npm install -g tsx
  • Use in npm scripts: "dev": "tsx watch src/index.ts" for live-reloading development
  • Pass Node flags directly: tsx --inspect src/server.ts for debugger support
  • No tsconfig.json required for basic usage; esbuild handles modern TypeScript syntax automatically
  • Use as a Node.js loader: node --import tsx ./script.ts for integration with other tooling

Key Features

  • Near-instant startup thanks to esbuild's native Go transpiler
  • Zero configuration needed for TypeScript, JSX, and path alias support
  • Built-in watch mode with smart file change detection
  • Supports the latest TypeScript and ECMAScript syntax features
  • Small dependency footprint compared to full TypeScript compiler setups

Comparison with Similar Tools

  • ts-node — more configurable with type checking support; tsx is faster for scripts that skip type checking
  • Bun — built-in TypeScript support with its own runtime; tsx stays on standard Node.js
  • esbuild-register — similar approach but tsx adds watch mode and better CLI ergonomics
  • Deno — native TypeScript execution; tsx keeps compatibility with the npm ecosystem
  • swc-node — SWC-based transpilation; tsx uses esbuild and provides a more complete CLI experience

FAQ

Q: Does tsx type-check my code? A: No. tsx only transpiles. Run tsc --noEmit separately in CI or use your editor for type checking.

Q: Can I use tsx in production? A: tsx is optimized for development. For production, pre-compile with tsc or bundle with esbuild for optimal startup and smaller images.

Q: Does tsx support path aliases from tsconfig.json? A: Yes. tsx reads paths from your tsconfig.json and resolves them automatically during module loading.

Q: How does watch mode work? A: tsx watch monitors imported files for changes and restarts the process. It is faster than nodemon because it only watches files actually imported by your application.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产