TypeScript Execute (tsx): Node.js enhanced with esbuild to run TypeScript & ESM files
npx @tessl/cli install tessl/npm-tsx@4.20.0tsx (TypeScript Execute) is a TypeScript execution runtime and CLI tool that enables developers to directly run TypeScript and ESM files in Node.js without requiring a separate compilation step. Built on top of esbuild for fast transpilation, it provides seamless execution of TypeScript code with full support for both CommonJS and ESM modules.
npm install tsxFor CommonJS environments:
const { register, require } = require("tsx/cjs/api");For ESM environments:
import { register, tsImport } from "tsx/esm/api";Direct CLI usage:
npx tsx script.ts# Run TypeScript files directly
tsx hello.ts
# Watch mode for development
tsx watch server.ts
# REPL mode
tsx
# Eval mode
tsx -e "console.log('Hello TypeScript')"const { register } = require("tsx/cjs/api");
// Register tsx loader
const unregister = register();
// Now you can require TypeScript files
const myModule = require("./my-module.ts");
// Unregister when done
unregister();import { register, tsImport } from "tsx/esm/api";
// Register tsx loader for ESM
const unregister = register();
// Import TypeScript modules
const myModule = await tsImport("./my-module.ts", import.meta.url);
// Unregister when done
await unregister();tsx is built around several key components:
Command-line interface for direct TypeScript execution with watch mode, REPL, and evaluation capabilities.
tsx [options] [script] [script-args...]
tsx watch [options] [script] [script-args...]Registration system for enabling TypeScript support in CommonJS modules with scoped require functionality.
const { register, require } = require("tsx/cjs/api");
function register(options?: RegisterOptions): Unregister;
function register(options: RequiredProperty<RegisterOptions, 'namespace'>): NamespacedUnregister;
function require(id: string, fromFile: string | URL): any;Hook-based loader system for ESM environments with dynamic import capabilities and advanced configuration options.
import { register, tsImport, type RegisterOptions } from "tsx/esm/api";
function register(options?: RegisterOptions): Unregister;
function register(options: RequiredProperty<RegisterOptions, 'namespace'>): NamespacedUnregister;
function tsImport(specifier: string, options: string | TsImportOptions): Promise<any>;
interface RegisterOptions {
namespace?: string;
onImport?: (url: string) => void;
tsconfig?: TsconfigOptions;
}
type TsconfigOptions = false | string;
type Unregister = () => Promise<void>;
interface TsImportOptions {
parentURL: string;
onImport?: (url: string) => void;
tsconfig?: TsconfigOptions;
}Default export that automatically registers both CommonJS and ESM transformation hooks.
import "tsx";
// or
require("tsx");This automatically enables TypeScript transformation for the entire process without explicit registration calls.
Suppress Node.js experimental feature warnings related to ESM loaders and import assertions.
require("tsx/suppress-warnings");Note: This entry point is deprecated and will be removed in the next major version. Use tsx/preflight instead.
Preflight module that sets up tsx for use with Node.js --require flag, enabling TypeScript support for subsequent --require modules.
require("tsx/preflight");Usage with Node.js flags:
node --require tsx/preflight --require ./my-typescript-config.ts app.jsThis module:
Patch Node.js REPL to support TypeScript syntax in interactive mode.
require("tsx/patch-repl");When required, this automatically patches the Node.js REPL to support TypeScript transformation.
Standalone TypeScript REPL that can be executed directly.
// Available as entry point: tsx/replNote: This entry point is deprecated and will be removed in the next major version. Use tsx command without arguments for REPL mode instead.
Usage:
node tsx/repl
# or via direct execution of tsx/repl entry pointtype RequiredProperty<Type, Keys extends keyof Type> = Type & { [P in Keys]-?: Type[P] };
type NodeError = Error & {
code: string;
url?: string;
path?: string;
};