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.
npm install typescript-registerVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to activate the TypeScript require hook and import a `.ts` file in a CommonJS Node.js application, including basic environment variable configuration and an example `.ts` file.
Consider modern alternatives like `ts-node`, `esbuild-register`, or `tsx` for on-the-fly TypeScript execution, which are actively maintained and support current standards.
Set environment variables before `require('typescript-register')`. For complex `compilerOptions`, ensure the JSON stringification is valid. For programmatic control, you might need to manage `process.env` directly or pre-process the script.Avoid using this package in environments where multiple custom `require` hooks are active. Test thoroughly to ensure compatibility with your specific toolchain. Modern alternatives often provide more isolated or controlled execution contexts.
For ESM projects, use modern TypeScript execution tools like `ts-node` configured for ESM, `tsx`, or `esbuild-register` which are built to handle Node.js's dual-package hazard and ESM loading mechanisms.
`npm install typescript` or `yarn add typescript` in your project.
Ensure 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.
This 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.