← 返回命令列表

Linux command

ts-node 命令

网络

复制后可按需替换文件名、目录或参数。

常用示例

Run TypeScript file

ts-node [script.ts]

Start REPL

ts-node

Run with specific config

ts-node -P [tsconfig.json] [script.ts]

Transpile only

ts-node --transpile-only [script.ts]

Run as ESM

ts-node --esm [script.ts]

Evaluate code

ts-node -e "console.log('Hello')"

Transpile with SWC

ts-node --swc [script.ts]

Print evaluated expression

ts-node -p "1 + 1"

说明

ts-node is a TypeScript execution engine for Node.js that compiles and runs TypeScript files on-the-fly without a separate build step. By default it performs full type checking at runtime, reporting type errors before execution begins. The --transpile-only mode skips type checking for significantly faster startup, which is useful during development when your editor already provides type feedback. The --swc flag uses the SWC transpiler (written in Rust) for an even greater speed improvement, and implies --transpileOnly. ESM mode (--esm) handles ES module imports and is needed when working with import/export syntax natively. The built-in REPL provides an interactive TypeScript environment for experimentation. Configuration is loaded from tsconfig.json by default, and a custom config can be specified with -P. The tool integrates well with development workflows using ts-node-dev or nodemon for automatic restart on file changes.

参数

-P _FILE_
Path to tsconfig.json.
--transpile-only, -T
Skip type checking.
--esm
Use ESM loader.
-e _CODE_
Evaluate code.
-p _CODE_
Evaluate and print.
-r _MODULE_
Require module.
--pretty
Pretty-print errors.
--skip-project
Skip loading tsconfig.json.
--swc
Transpile with SWC for faster startup. Implies --transpileOnly.
--files
Load files, include, and exclude from tsconfig.json on startup.
-C _NAME_, --compiler _NAME_
TypeScript compiler to use (default: typescript).
-D _CODES_, --ignoreDiagnostics _CODES_
Ignore TypeScript diagnostics by code.
--emit
Write compiled output files to .ts-node directory.
-i, --interactive
Force REPL even if stdin is not a terminal.
--skipIgnore
Skip ignore checks, allowing compilation of files in node_modules.
-O _JSON_, --compilerOptions _JSON_
Merge JSON compiler options with tsconfig.
--showConfig
Print resolved tsconfig.json and exit.

FAQ

What is the ts-node command used for?

ts-node is a TypeScript execution engine for Node.js that compiles and runs TypeScript files on-the-fly without a separate build step. By default it performs full type checking at runtime, reporting type errors before execution begins. The --transpile-only mode skips type checking for significantly faster startup, which is useful during development when your editor already provides type feedback. The --swc flag uses the SWC transpiler (written in Rust) for an even greater speed improvement, and implies --transpileOnly. ESM mode (--esm) handles ES module imports and is needed when working with import/export syntax natively. The built-in REPL provides an interactive TypeScript environment for experimentation. Configuration is loaded from tsconfig.json by default, and a custom config can be specified with -P. The tool integrates well with development workflows using ts-node-dev or nodemon for automatic restart on file changes.

How do I run a basic ts-node example?

Run `ts-node [script.ts]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -P _FILE_ do in ts-node?

Path to tsconfig.json.