TypeScript Register
TypeScript Register is a utility library designed to enable direct `require()` calls for TypeScript files within Node.js applications, bypassing the need for a separate pre-compilation step. This package offers an alternative approach to other similar tools by integrating directly with the official TypeScript API rather than forking `tsc`, ensuring dependencies run within the same context as the parent module. For performance, it implements a caching mechanism, storing compiled outputs in a temporary directory (e.g., `/tmp/typescript-register/...`). Configuration options, including error emission, cache utilization, and custom TypeScript compiler options, are managed exclusively via environment variables. While functional for immediate TypeScript execution in Node.js, it has seen no significant updates since version 1.1.0, published in 2018, making its compatibility with modern Node.js and TypeScript versions, as well as ESM modules, uncertain. Its primary use case was for simplifying development and testing workflows in a CommonJS-centric Node.js ecosystem.
Common errors
-
Error: Cannot find module 'typescript' or its corresponding type declarations.
cause The `typescript` package, which is a required runtime dependency for compilation, is missing from the project's `node_modules`.fix`npm install typescript` or `yarn add typescript` in your project. -
SyntaxError: Unexpected token 'export' (at ...)
-
SyntaxError: Unexpected token 'import' (at ...)
cause Attempting to run a TypeScript file using ESM `import`/`export` syntax within an older Node.js version or a CommonJS context where `typescript-register` is not properly transpiling to CJS, or where the Node.js runtime itself expects CJS.fixEnsure your TypeScript files are compiling to CommonJS (if you intend to use `require`), or switch to a modern runner like `tsx` or `ts-node` with ESM support for projects using native ESM. -
TypeError: require.extensions is not a function
cause Newer Node.js versions have deprecated or changed how `require.extensions` works, or the execution environment (e.g., bundled runtime) has restricted access to it, which this package heavily relies upon.fixThis package heavily relies on `require.extensions` and is unlikely to work in environments where it's unavailable or modified. Migrate to a modern alternative that uses Node.js's official loader hooks or a more robust transpilation approach.
Warnings
- breaking This package has been abandoned since 2018 (last publish v1.1.0) and is highly unlikely to be compatible with recent versions of Node.js (e.g., Node 16+), TypeScript (e.g., TS 4+), or native ESM modules. Using it in modern projects will likely lead to compilation failures or runtime errors.
- gotcha Configuration for `typescript-register` is exclusively handled through environment variables (`TYPESCRIPT_REGISTER_EMIT_ERROR`, `TYPESCRIPT_REGISTER_USE_CACHE`, `TYPESCRIPT_REGISTER_COMPILER_OPTIONS`). There is no programmatic API or `tsconfig.json` integration, making dynamic configuration complex and potentially error-prone for larger projects.
- gotcha The package registers a global `require` hook. While convenient, this approach can conflict with other custom module loaders, bundlers, or environments that modify Node.js's module resolution, potentially leading to unexpected behavior or difficult-to-debug issues.
- breaking This package is designed for CommonJS environments and does not natively support ECMAScript Modules (ESM) syntax (`import`/`export` outside of transpilation to CJS) or the `.mjs`/`.mts` file extensions directly. Attempting to use it in an ESM project will likely fail.
Install
-
npm install typescript-register -
yarn add typescript-register -
pnpm add typescript-register
Imports
- registerHook
import 'typescript-register';
require('typescript-register');
Quickstart
{
// bar.js
process.env.TYPESCRIPT_REGISTER_EMIT_ERROR = 'true';
process.env.TYPESCRIPT_REGISTER_USE_CACHE = 'true';
// Optional: Configure TypeScript compiler options. This example targets ES2018
// and uses CommonJS modules, with strict type checking enabled.
process.env.TYPESCRIPT_REGISTER_COMPILER_OPTIONS = JSON.stringify({
target: 'es2018',
module: 'commonjs',
strict: true,
esModuleInterop: true // Often needed for better interop with CommonJS
});
// Activate the TypeScript require hook. All subsequent `require` calls for .ts files
// will be transpiled on the fly.
require('typescript-register');
// Attempt to require a TypeScript file.
try {
// Ensure 'foo.ts' exists in the same directory as 'bar.js'
// Example content for 'foo.ts': `export var foo = 3; export const greeting = "Hello";`
const fooModule = require('./foo.ts');
console.log('Value from foo.ts:', fooModule.foo); // Expected: 3
console.log('Greeting from foo.ts:', fooModule.greeting); // Expected: "Hello"
} catch (e) {
console.error('Error requiring TypeScript file:', e.message);
if (e.stack) {
console.error(e.stack);
}
}
// Additional example: a type-checked function in TypeScript
/*
// In a file named 'calculator.ts'
export function add(a: number, b: number): number {
return a + b;
}
*/
try {
const calculator = require('./calculator.ts');
console.log('2 + 3 =', calculator.add(2, 3)); // Expected: 5
// This would typically cause a TypeScript error during compilation if type-checked:
// console.log('2 + "3" =', calculator.add(2, "3"));
} catch (e) {
console.error('Error requiring calculator.ts:', e.message);
}}