TypeScript Register

1.1.0 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

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.

{
// 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);
}}

view raw JSON →