TypeScript execution environment and REPL for Node.js with source map support
—
Comprehensive configuration system supporting all TypeScript compiler options plus ts-node specific features for customizing compilation behavior.
Core configuration options for creating ts-node services.
interface CreateOptions {
/**
* Behave as if invoked within this working directory
* @default process.cwd()
*/
cwd?: string;
/**
* Legacy alias for cwd
* @deprecated use projectSearchDir or cwd
*/
dir?: string;
/**
* Emit output files into .ts-node directory
* @default false
*/
emit?: boolean;
/**
* Scope compiler to files within scopeDir
* @default false
*/
scope?: boolean;
/**
* Directory to scope compilation to
* @default First of: tsconfig.json "rootDir", directory containing tsconfig.json, or cwd
*/
scopeDir?: string;
/**
* Use pretty diagnostic formatter
* @default false
*/
pretty?: boolean;
/**
* Use TypeScript's faster transpileModule
* @default false
*/
transpileOnly?: boolean;
/**
* Specify type-check is enabled (e.g. transpileOnly == false)
* @deprecated
* @default true
*/
typeCheck?: boolean;
/**
* Use TypeScript's compiler host API instead of language service API
* @default false
*/
compilerHost?: boolean;
/**
* Logs TypeScript errors to stderr instead of throwing exceptions
* @default false
*/
logError?: boolean;
/**
* Load "files" and "include" from tsconfig.json on startup
* @default false
*/
files?: boolean;
/**
* Specify a custom TypeScript compiler
* @default "typescript"
*/
compiler?: string;
/**
* Specify a custom transpiler for use with transpileOnly
*/
transpiler?: string | [string, object];
/**
* Transpile with swc instead of TypeScript compiler, and skip typechecking
* Equivalent to setting both transpileOnly: true and transpiler: 'ts-node/transpilers/swc'
*/
swc?: boolean;
/**
* Paths which should not be compiled
* Each string is converted to RegExp and tested against source paths
* @default ["(?:^|/)node_modules/"]
*/
ignore?: string[];
/**
* Path to TypeScript config file or directory containing tsconfig.json
* Similar to tsc --project flag
*/
project?: string;
/**
* Search for TypeScript config file in this or parent directories
*/
projectSearchDir?: string;
/**
* Skip project config resolution and loading
* @default false
*/
skipProject?: boolean;
/**
* Skip ignore check, so compilation is attempted for all files with matching extensions
* @default false
*/
skipIgnore?: boolean;
/**
* JSON object to merge with TypeScript compilerOptions
*/
compilerOptions?: object;
/**
* Ignore TypeScript warnings by diagnostic code
*/
ignoreDiagnostics?: Array<number | string>;
/**
* Modules to require, like node's --require flag
* If specified in tsconfig.json, modules are resolved relative to the tsconfig.json file
*/
require?: Array<string>;
/**
* File existence check function override
*/
fileExists?: (path: string) => boolean;
/**
* File reading function override
*/
readFile?: (path: string) => string | undefined;
/**
* Custom TypeScript transformers
*/
transformers?: CustomTransformers | ((program: Program) => CustomTransformers);
/**
* Allows usage of top level await in REPL
* Uses node's implementation with AST syntax transformation
* Enabled by default when tsconfig target is es2018 or above
*/
experimentalReplAwait?: boolean;
/**
* Override certain paths to be compiled as CommonJS or ECMAScript modules
* When overridden, tsconfig "module" and package.json "type" fields are overridden
*/
moduleTypes?: ModuleTypes;
/**
* Function to collect trace messages from TypeScript compiler
* @default console.log
*/
tsTrace?: (str: string) => void;
/**
* Enable native ESM support
* For details, see https://typestrong.org/ts-node/docs/imports#native-ecmascript-modules
*/
esm?: boolean;
/**
* Re-order file extensions so TypeScript imports are preferred
* When both index.js and index.ts exist, enabling causes require('./index') to resolve to index.ts
* @default false
*/
preferTsExts?: boolean;
/**
* Like node's --experimental-specifier-resolution, but can be set in tsconfig.json
* For details, see https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#customizing-esm-specifier-resolution-algorithm
*/
experimentalSpecifierResolution?: 'node' | 'explicit';
/**
* Allow using voluntary .ts file extension in import specifiers
* Typically, ESM projects must have emit extensions (.js, .cjs, .mjs)
* This flag allows using .ts extensions directly
*/
experimentalTsImportSpecifiers?: boolean;
}Extended options for registering ts-node globally.
interface RegisterOptions extends CreateOptions {
/**
* Enable experimental features that re-map imports and require calls to support:
* baseUrl, paths, rootDirs, .js to .ts file extension mappings,
* outDir to rootDir mappings for composite projects and monorepos
* For details, see https://github.com/TypeStrong/ts-node/issues/1514
*/
experimentalResolver?: boolean;
}/**
* Override module types for specific file patterns
*/
type ModuleTypes = Record<string, ModuleTypeOverride>;
/**
* Module type override options
*/
type ModuleTypeOverride = 'cjs' | 'esm' | 'package';Usage Examples:
import { register } from "ts-node";
// Override specific files to be treated as CommonJS
register({
moduleTypes: {
"**/*.spec.ts": "cjs", // All test files as CommonJS
"src/legacy/**/*.ts": "cjs", // Legacy code as CommonJS
"src/modern/**/*.ts": "esm", // Modern code as ESM
}
});/**
* TypeScript configuration options interface
* Compatible with typescript-json-schema for tsconfig.json validation
*/
interface TsConfigOptions extends Omit<RegisterOptions,
| 'transformers'
| 'readFile'
| 'fileExists'
| 'skipProject'
| 'project'
| 'dir'
| 'cwd'
| 'projectSearchDir'
| 'optionBasePaths'
| 'tsTrace'
> {}/**
* Default register options, including values from environment variables
*/
const DEFAULTS: RegisterOptions;Environment Variables:
ts-node reads configuration from these environment variables:
interface ProcessEnv {
TS_NODE_DEBUG?: string;
TS_NODE_CWD?: string;
TS_NODE_DIR?: string; // @deprecated
TS_NODE_EMIT?: string;
TS_NODE_SCOPE?: string;
TS_NODE_SCOPE_DIR?: string;
TS_NODE_FILES?: string;
TS_NODE_PRETTY?: string;
TS_NODE_COMPILER?: string;
TS_NODE_COMPILER_OPTIONS?: string;
TS_NODE_IGNORE?: string;
TS_NODE_PROJECT?: string;
TS_NODE_SKIP_PROJECT?: string;
TS_NODE_SKIP_IGNORE?: string;
TS_NODE_PREFER_TS_EXTS?: string;
TS_NODE_IGNORE_DIAGNOSTICS?: string;
TS_NODE_TRANSPILE_ONLY?: string;
TS_NODE_TYPE_CHECK?: string;
TS_NODE_COMPILER_HOST?: string;
TS_NODE_LOG_ERROR?: string;
TS_NODE_HISTORY?: string;
TS_NODE_EXPERIMENTAL_REPL_AWAIT?: string;
NODE_NO_READLINE?: string;
}import { register } from "ts-node";
// Fast development setup with transpile-only
register({
transpileOnly: true,
compilerOptions: {
target: "es2020",
module: "commonjs",
strict: true,
esModuleInterop: true,
},
});import { register } from "ts-node";
// Full type checking for production builds
register({
typeCheck: true,
pretty: true,
files: true,
compilerOptions: {
target: "es2018",
module: "commonjs",
strict: true,
noUnusedLocals: true,
noUnusedParameters: true,
},
ignoreDiagnostics: [2307], // Ignore module resolution errors
});import { register } from "ts-node";
// Native ESM support
register({
esm: true,
experimentalSpecifierResolution: "node",
compilerOptions: {
target: "es2020",
module: "esnext",
moduleResolution: "node",
},
});import { register } from "ts-node";
// Use SWC for faster compilation
register({
swc: true, // Implies transpileOnly: true
compilerOptions: {
target: "es2020",
module: "commonjs",
},
});Install with Tessl CLI
npx tessl i tessl/npm-ts-node