{"id":12250,"library":"typescript-register","title":"TypeScript Register","description":"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.","status":"abandoned","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/pspeter3/typescript-register","tags":["javascript","typescript","require","interpret"],"install":[{"cmd":"npm install typescript-register","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-register","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-register","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required at runtime for compiling TypeScript files on-the-fly, though it's often an implicit peer dependency rather than a direct one in `package.json`.","package":"typescript","optional":false}],"imports":[{"note":"This package registers a global `require` hook and does not export any named symbols. It is primarily designed for CommonJS environments and does not support ESM `import` statements.","wrong":"import 'typescript-register';","symbol":"registerHook","correct":"require('typescript-register');"}],"quickstart":{"code":"{\n// bar.js\nprocess.env.TYPESCRIPT_REGISTER_EMIT_ERROR = 'true';\nprocess.env.TYPESCRIPT_REGISTER_USE_CACHE = 'true';\n// Optional: Configure TypeScript compiler options. This example targets ES2018\n// and uses CommonJS modules, with strict type checking enabled.\nprocess.env.TYPESCRIPT_REGISTER_COMPILER_OPTIONS = JSON.stringify({\n  target: 'es2018',\n  module: 'commonjs',\n  strict: true,\n  esModuleInterop: true // Often needed for better interop with CommonJS\n});\n\n// Activate the TypeScript require hook. All subsequent `require` calls for .ts files\n// will be transpiled on the fly.\nrequire('typescript-register');\n\n// Attempt to require a TypeScript file.\ntry {\n  // Ensure 'foo.ts' exists in the same directory as 'bar.js'\n  // Example content for 'foo.ts': `export var foo = 3; export const greeting = \"Hello\";`\n  const fooModule = require('./foo.ts');\n  console.log('Value from foo.ts:', fooModule.foo); // Expected: 3\n  console.log('Greeting from foo.ts:', fooModule.greeting); // Expected: \"Hello\"\n} catch (e) {\n  console.error('Error requiring TypeScript file:', e.message);\n  if (e.stack) {\n    console.error(e.stack);\n  }\n}\n\n// Additional example: a type-checked function in TypeScript\n/*\n// In a file named 'calculator.ts'\nexport function add(a: number, b: number): number {\n  return a + b;\n}\n*/\ntry {\n  const calculator = require('./calculator.ts');\n  console.log('2 + 3 =', calculator.add(2, 3)); // Expected: 5\n  // This would typically cause a TypeScript error during compilation if type-checked:\n  // console.log('2 + \"3\" =', calculator.add(2, \"3\"));\n} catch (e) {\n  console.error('Error requiring calculator.ts:', e.message);\n}}","lang":"javascript","description":"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."},"warnings":[{"fix":"Consider modern alternatives like `ts-node`, `esbuild-register`, or `tsx` for on-the-fly TypeScript execution, which are actively maintained and support current standards.","message":"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.","severity":"breaking","affected_versions":">=1.1.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"`npm install typescript` or `yarn add typescript` in your project.","cause":"The `typescript` package, which is a required runtime dependency for compilation, is missing from the project's `node_modules`.","error":"Error: Cannot find module 'typescript' or its corresponding type declarations."},{"error":"SyntaxError: Unexpected token 'export' (at ...)"},{"fix":"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.","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.","error":"SyntaxError: Unexpected token 'import' (at ...)"},{"fix":"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.","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.","error":"TypeError: require.extensions is not a function"}],"ecosystem":"npm"}