TypeScript execution environment and REPL for Node.js with source map support
npx @tessl/cli install tessl/npm-ts-node@10.9.0ts-node is a TypeScript execution environment and REPL for Node.js that enables direct execution of TypeScript files without precompilation. It works by JIT-transforming TypeScript into JavaScript using Node.js module loading APIs, making it seamlessly integrate with other Node.js tools and libraries.
npm install ts-nodeimport { register, create, Service, CreateOptions } from "ts-node";
import type * as _ts from "typescript";For CommonJS:
const { register, create } = require("ts-node");import { register } from "ts-node";
// Register ts-node globally to handle .ts files
const service = register({
transpileOnly: true,
compilerOptions: {
target: "es2020",
module: "commonjs",
},
});
// Now you can require/import .ts files directly
const myModule = require("./my-typescript-file.ts");ts-node is built around several key components:
Service, register, create)Core functionality for creating and registering TypeScript compilation services. This is the primary API for programmatic usage.
function register(opts?: RegisterOptions): Service;
function register(service: Service): Service;
function create(rawOptions?: CreateOptions): Service;
interface Service {
ts: TSCommon;
config: _ts.ParsedCommandLine;
options: RegisterOptions;
enabled(enabled?: boolean): boolean;
ignored(fileName: string): boolean;
compile(code: string, fileName: string, lineOffset?: number): string;
getTypeInfo(code: string, fileName: string, position: number): TypeInfo;
}Comprehensive configuration system supporting all TypeScript compiler options plus ts-node specific features.
interface CreateOptions {
cwd?: string;
emit?: boolean;
scope?: boolean;
scopeDir?: string;
pretty?: boolean;
transpileOnly?: boolean;
typeCheck?: boolean;
compilerHost?: boolean;
logError?: boolean;
files?: boolean;
compiler?: string;
transpiler?: string | [string, object];
swc?: boolean;
ignore?: string[];
project?: string;
projectSearchDir?: string;
skipProject?: boolean;
skipIgnore?: boolean;
compilerOptions?: object;
ignoreDiagnostics?: Array<number | string>;
require?: Array<string>;
experimentalReplAwait?: boolean;
moduleTypes?: ModuleTypes;
esm?: boolean;
preferTsExts?: boolean;
experimentalSpecifierResolution?: 'node' | 'explicit';
experimentalTsImportSpecifiers?: boolean;
}Interactive TypeScript REPL with evaluation context management and experimental top-level await support.
function createRepl(options?: CreateReplOptions): ReplService;
interface ReplService {
evalCode(code: string, context: object): any;
start(): void;
nodeEval(code: string, context: object, filename: string, callback: Function): void;
}Native ECMAScript module support with loader hooks for seamless TypeScript execution in ESM contexts.
function createEsmHooks(tsNodeService: Service): EsmHooks;
function registerAndCreateEsmHooks(opts?: RegisterOptions): EsmHooks;
interface NodeLoaderHooksAPI2 {
resolve(specifier: string, context: object, next: Function): Promise<object>;
load(url: string, context: object, next: Function): Promise<object>;
}Convenient entry points for different registration modes, designed for use with Node.js --require flag.
// Available entry points:
// ts-node/register - Default registration
// ts-node/register/transpile-only - Fast transpile-only mode
// ts-node/register/type-check - Explicit type checking mode
// ts-node/register/files - Include tsconfig files modeCommand-line interfaces for executing TypeScript files and interactive REPL sessions.
// Available CLI binaries:
// ts-node - Main CLI with REPL support
// ts-node-cwd - CLI with working directory change
// ts-node-esm - CLI with native ESM support
// ts-node-script - Script execution without REPL
// ts-node-transpile-only - Fast transpile-only CLI
// ts-script - Deprecated aliasSupport for alternative transpilers like SWC for faster compilation and experimental features.
interface TranspilerModule {
create(options: CreateTranspilerOptions): Transpiler;
}
interface Transpiler {
transpile(code: string, options: TranspileOptions): TranspileOutput;
}interface TypeInfo {
name: string;
comment: string;
}
interface RegisterOptions extends CreateOptions {
experimentalResolver?: boolean;
}
type ModuleTypes = Record<string, ModuleTypeOverride>;
type ModuleTypeOverride = 'cjs' | 'esm' | 'package';
type ExperimentalSpecifierResolution = 'node' | 'explicit';
/** @deprecated Use Service instead */
type Register = Service;
/** @internal TypeScript configuration options interface */
interface TsConfigOptions extends Omit<RegisterOptions,
| 'transformers'
| 'readFile'
| 'fileExists'
| 'skipProject'
| 'project'
| 'dir'
| 'cwd'
| 'projectSearchDir'
| 'optionBasePaths'
| 'tsTrace'
> {}
/** @internal Diagnostic filter interface */
interface DiagnosticFilter {
appliesToAllFiles: boolean;
filenamesAbsolute: string[];
diagnosticsIgnored: number[];
}
/** @internal Node.js module emit types */
type NodeModuleEmitKind = 'nodeesm' | 'nodecjs';
class TSError extends Error {
name: 'TSError';
diagnosticText: string;
diagnostics: ReadonlyArray<_ts.Diagnostic>;
diagnosticCodes: number[];
}
/** TypeScript compiler interface */
interface TSCommon {
version: string;
sys: _ts.System;
ScriptTarget: typeof _ts.ScriptTarget;
ModuleKind: typeof _ts.ModuleKind;
JsxEmit: typeof _ts.JsxEmit;
createProgram: typeof _ts.createProgram;
transpileModule: typeof _ts.transpileModule;
getDefaultLibFileName: typeof _ts.getDefaultLibFileName;
// ... other TypeScript compiler APIs
}const VERSION: string;
const REGISTER_INSTANCE: symbol;
const DEFAULTS: RegisterOptions;
/** @internal Debug logging function */
const debug: (...args: any[]) => void;
/** @internal Typed process.env interface */
const env: ProcessEnv;
/** @internal Custom inspect symbol */
const INSPECT_CUSTOM: string | symbol;