ts-node
Execute TypeScript code directly
TLDR
Execute a TypeScript file without compiling (node + tsc)
Execute a TypeScript file without loading tsconfig.json
Evaluate TypeScript code passed as a literal
Execute a TypeScript file in script mode
Transpile a TypeScript file to JavaScript without executing it
Display help
SYNOPSIS
ts-node [options] [file or script] [arguments...]
ts-node -e "code"
PARAMETERS
-P, --project <path>
Specify the path to the tsconfig.json
file. By default, ts-node searches for tsconfig.json
in the current working directory.-T, --transpile-only
Disable type checking during compilation. This can significantly speed up execution for large projects but means type errors will not be reported by ts-node.-r, --require <module>
Require a module or file before executing the main script, similar to Node.js's --require
flag. Useful for polyfills or global setup.-e, --eval <code>
Evaluate the provided TypeScript code directly as a string.--cwd <path>
Set the current working directory for ts-node, affecting module resolution and tsconfig.json
discovery.--files
Load all files included in the files
array of your tsconfig.json
, which are usually ignored by default for performance reasons.--esm
Enable experimental ES Modules support for Node.js and TypeScript. This uses Node.js's native ESM loader.--compiler <name>
Specify a custom TypeScript compiler module to use instead of the default typescript
package.--pretty
Use color and style for diagnostic messages and errors, improving readability.--skip-project
Completely skip tsconfig.json
discovery and usage. ts-node will fall back to default compiler options.
DESCRIPTION
ts-node is a TypeScript execution environment for Node.js, enabling developers to run TypeScript files directly without prior compilation to JavaScript. This significantly accelerates the development workflow, particularly for scripting, testing, and development server execution. It seamlessly integrates with Node.js's module resolution system, allowing the use of require()
and import
statements. By performing on-the-fly transpilation, ts-node eliminates the need for a separate build step during development, providing a rapid feedback loop. It supports a wide array of TypeScript features and Node.js versions, making it an indispensable tool for modern TypeScript projects.
CAVEATS
Performance Overhead: While convenient for development, ts-node introduces a compilation overhead. For production deployments, it's generally recommended to pre-compile TypeScript code to JavaScript using tsc
for optimal performance.
Type Checking vs. Speed: By default, ts-node performs type checking, which can be slow for very large projects. The --transpile-only
flag can mitigate this by skipping type checks, but you lose immediate type error feedback during runtime.
Module Resolution with Paths: Configuring module resolution, especially with paths
mapping in tsconfig.json
, can sometimes require additional setup (e.g., using tsconfig-paths
alongside ts-node) to work correctly.
CONFIGURATION
ts-node automatically loads and respects the compiler options defined in a tsconfig.json
file found in the current directory or specified via the --project
flag. This includes settings like target
, module
, strict
, and others, which directly influence how ts-node transpiles your code.
ENVIRONMENT VARIABLES
Many ts-node options can also be configured via environment variables, prefixed with TS_NODE_
(e.g., TS_NODE_PROJECT
, TS_NODE_TRANSPILE_ONLY
, TS_NODE_COMPILER_OPTIONS
). This provides flexibility for CI/CD pipelines, Docker environments, or specific script configurations without modifying command-line arguments.
HISTORY
Before ts-node, developers often had to compile their TypeScript code to JavaScript with tsc
after every change to run it, leading to a cumbersome and slow development cycle. ts-node was created to streamline this process by providing an on-the-fly transpilation layer, allowing TypeScript files to be executed directly, much like how Node.js executes JavaScript or Babel-node handles modern JavaScript features. It quickly became a de facto standard tool for rapid TypeScript development, testing, and scripting, significantly improving developer productivity and feedback loops.