TypeScript execution environment and REPL for Node.js with source map support
—
Core functionality for creating and registering TypeScript compilation services. This is the primary API for programmatic usage of ts-node.
Creates and registers a TypeScript compiler instance globally on the Node.js runtime.
/**
* Create a new TypeScript compiler instance and register it onto node.js
* @param opts - Configuration options for the service
* @returns Service instance for compilation and management
*/
function register(opts?: RegisterOptions): Service;
/**
* Register an existing TypeScript compiler service onto node.js
* @param service - Pre-existing Service instance to register
* @returns The same Service instance
*/
function register(service: Service): Service;Usage Examples:
import { register } from "ts-node";
// Basic registration with default options
const service = register();
// Registration with custom options
const service = register({
transpileOnly: true,
compilerOptions: {
target: "es2020",
module: "commonjs",
},
ignore: [/node_modules/],
});
// Register an existing service
const existingService = create({ transpileOnly: true });
register(existingService);Creates a TypeScript compiler instance without registering it globally.
/**
* Create TypeScript compiler instance without global registration
* @param rawOptions - Configuration options for the service
* @returns Service instance for compilation
*/
function create(rawOptions?: CreateOptions): Service;Usage Examples:
import { create } from "ts-node";
// Create service for manual compilation
const service = create({
transpileOnly: true,
compilerOptions: {
target: "es2018",
strict: true,
},
});
// Compile TypeScript code manually
const result = service.compile('const x: number = 42;', 'test.ts');
console.log(result); // Compiled JavaScriptThe primary interface for TypeScript compilation and management operations.
interface Service {
/** TypeScript compiler instance */
ts: TSCommon;
/** Parsed TypeScript configuration */
config: _ts.ParsedCommandLine;
/** Service configuration options */
options: RegisterOptions;
/**
* Enable or disable the service
* @param enabled - Whether to enable the service
* @returns Current enabled state
*/
enabled(enabled?: boolean): boolean;
/**
* Check if a filename should be ignored by ts-node
* @param fileName - File path to check
* @returns True if file should be ignored
*/
ignored(fileName: string): boolean;
/**
* Compile TypeScript code to JavaScript
* @param code - TypeScript source code
* @param fileName - Source file name for error reporting
* @param lineOffset - Line number offset for error reporting
* @returns Compiled JavaScript code
*/
compile(code: string, fileName: string, lineOffset?: number): string;
/**
* Get type information at a specific position in TypeScript code
* @param code - TypeScript source code
* @param fileName - Source file name
* @param position - Character position in code
* @returns Type information object
*/
getTypeInfo(code: string, fileName: string, position: number): TypeInfo;
// Internal properties and methods
/** @internal Service brand for type checking */
[TS_NODE_SERVICE_BRAND]: true;
/** @internal TypeScript compiler path */
compilerPath: string;
/** @internal TypeScript config file path */
configFilePath: string | undefined;
/** @internal Module type classifier */
moduleTypeClassifier: ModuleTypeClassifier;
/** @internal Whether REPL await is supported */
readonly shouldReplAwait: boolean;
/** @internal Whether transpile-only mode is enabled */
transpileOnly: boolean;
/** @internal Project-local resolve helper */
projectLocalResolveHelper: ProjectLocalResolveHelper;
/** @internal File extensions configuration */
extensions: Extensions;
/** @internal Add diagnostic filter */
addDiagnosticFilter(filter: DiagnosticFilter): void;
/** @internal Install source map support */
installSourceMapSupport(): void;
/** @internal Enable experimental ESM loader interop */
enableExperimentalEsmLoaderInterop(): void;
/** @internal Get Node.js ESM resolver */
getNodeEsmResolver(): any;
/** @internal Get Node.js ESM format detector */
getNodeEsmGetFormat(): any;
/** @internal Get Node.js CJS loader */
getNodeCjsLoader(): any;
}
interface TypeInfo {
/** Type name */
name: string;
/** Documentation comment */
comment: string;
}Usage Examples:
import { create } from "ts-node";
const service = create({ transpileOnly: true });
// Check if service is enabled
console.log(service.enabled()); // true
// Check if a file should be ignored
console.log(service.ignored('node_modules/some-package/index.ts')); // true
console.log(service.ignored('src/app.ts')); // false
// Compile TypeScript code
const jsCode = service.compile(
'const message: string = "Hello, World!";',
'example.ts'
);
// Get type information
const typeInfo = service.getTypeInfo(
'const num: number = 42;',
'example.ts',
6 // Position of 'num'
);
console.log(typeInfo.name); // "number"Access to the globally registered ts-node instance.
/** Symbol for accessing registered instance on process object */
const REGISTER_INSTANCE: symbol;
// Global process interface extension
declare global {
namespace NodeJS {
interface Process {
[REGISTER_INSTANCE]?: Service;
}
}
}Usage Examples:
import { register, REGISTER_INSTANCE } from "ts-node";
// Register a service
register({ transpileOnly: true });
// Access the global service
const globalService = process[REGISTER_INSTANCE];
if (globalService) {
console.log('ts-node is registered globally');
const result = globalService.compile('const x = 1;', 'test.ts');
}TypeScript compilation errors and diagnostics.
class TSError extends Error {
name: 'TSError';
/** Formatted diagnostic text */
diagnosticText: string;
/** Raw TypeScript diagnostics */
diagnostics: ReadonlyArray<Diagnostic>;
/** Diagnostic error codes */
diagnosticCodes: number[];
constructor(
diagnosticText: string,
diagnosticCodes: number[],
diagnostics?: ReadonlyArray<Diagnostic>
);
}Usage Examples:
import { create, TSError } from "ts-node";
const service = create({ typeCheck: true });
try {
// This will throw TSError due to type mismatch
service.compile('const x: string = 42;', 'error.ts');
} catch (error) {
if (error instanceof TSError) {
console.log('TypeScript Error:', error.diagnosticText);
console.log('Error Codes:', error.diagnosticCodes);
console.log('Raw Diagnostics:', error.diagnostics);
}
}Install with Tessl CLI
npx tessl i tessl/npm-ts-node