{"id":14999,"library":"typescript-require","title":"TypeScript Require Extension for Node.js","description":"typescript-require is a Node.js `require` extension designed to enable direct loading of TypeScript (`.ts`) modules without a separate pre-compilation step. Upon initialization, it hooks into Node's module resolution, compiling `.ts` files on-the-fly when they are `require`d, resolving internal dependencies. Its current stable version is 0.3.0, released in 2012. Given its age and the evolution of the TypeScript ecosystem, it represents an early approach to running TypeScript directly in Node.js, predating more modern solutions like `ts-node` or native ESM support with `tsx`. Key differentiators at the time were its simplicity as a `require` hook and configurability for ES target and error handling, but it lacks support for modern TypeScript features or robust project configuration via `tsconfig.json`.","status":"abandoned","version":"0.3.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/theblacksmith/typescript-require","tags":["javascript","typescript","require"],"install":[{"cmd":"npm install typescript-require","lang":"bash","label":"npm"},{"cmd":"yarn add typescript-require","lang":"bash","label":"yarn"},{"cmd":"pnpm add typescript-require","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is a CommonJS `require` hook and does not support ES module `import` syntax for its own initialization. It enables `import` syntax for TS modules it processes.","wrong":"import 'typescript-require';","symbol":"TypeScriptRequire","correct":"require('typescript-require');"},{"note":"Configuration is passed directly as an argument to the initial `require` call, not via a separate import or method.","wrong":"import typescriptRequire from 'typescript-require';","symbol":"ConfiguredTypeScriptRequire","correct":"require('typescript-require')({ targetES5: true, exitOnError: false });"},{"note":"While the package enables requiring `.ts` files, the internal examples show `var foomodule = require('./foomodule.js');` and `import barmodule = module('barmodule');` for TS files, implying `require('./some-ts-file')` is the primary way to consume the compiled output in the calling JS code.","wrong":"import { myModule } from './myModule.ts';","symbol":"TSModuleImport","correct":"var myModule = require('./myModule.ts');"}],"quickstart":{"code":"/* app.js */\nconst path = require('path');\nconst fs = require('fs');\n\n// Create a temporary directory for output if it doesn't exist\nconst tmpDir = path.join(__dirname, 'tmp');\nif (!fs.existsSync(tmpDir)) {\n    fs.mkdirSync(tmpDir);\n}\n\n// Initialize typescript-require with custom options\nrequire('typescript-require')({\n    targetES5: true,\n    exitOnError: false, // Don't exit process immediately on error for demonstration\n    tmpDir: tmpDir // Specify temporary directory\n});\n\n// Create a sample TypeScript file dynamically for demonstration\nconst tsContent = `\n    export function lowercase(val: string): string {\n        return val.toLowerCase();\n    }\n\n    export function uppercase(val: string): string {\n        return val.toUpperCase();\n    }\n`;\nconst funcsPath = path.join(__dirname, 'funcs.ts');\nfs.writeFileSync(funcsPath, tsContent);\n\n// Require the TypeScript module\nconst funcs = require(funcsPath);\n\nconsole.log(funcs.lowercase(\"HELLO FROM TYPESCRIPT!\"));\nconsole.log(funcs.uppercase(\"hello from typescript!\"));\n\n// Clean up generated files and directory\nfs.unlinkSync(funcsPath);\n// Optional: Clean up compiled JS files and source maps in tmpDir as well\nfs.readdirSync(tmpDir).forEach(file => {\n    if (file.startsWith('funcs.') && (file.endsWith('.js') || file.endsWith('.map'))) {\n        fs.unlinkSync(path.join(tmpDir, file));\n    }\n});\nfs.rmdirSync(tmpDir);\n","lang":"javascript","description":"This quickstart demonstrates how to initialize `typescript-require` with configuration options and then dynamically create and require a TypeScript module, showing its on-the-fly compilation capabilities."},"warnings":[{"fix":"Migrate to `ts-node` for JIT compilation in CommonJS/ESM, or `tsx` for a more modern JIT execution environment. For production, pre-compile TypeScript using `tsc`.","message":"This package is largely incompatible with modern TypeScript features, `tsconfig.json` project configuration, or ES Modules (ESM). It was designed for an older TypeScript compiler API and CommonJS environments. Attempting to use it with recent TypeScript syntax or module systems will lead to compilation errors or unexpected behavior.","severity":"breaking","affected_versions":"All versions, especially with TypeScript >= 2.0"},{"fix":"If forced to use this package, manually add `/// <reference path='node.d.ts'/>` to relevant `.ts` files, or consider setting `nodeLib: true` in the configuration if available and functional.","message":"The `nodeLib` option is `false` by default, meaning `node.d.ts` definitions are not automatically loaded. This requires explicit `/// <reference path='node.d.ts'/>` comments in TypeScript files that use Node.js types, which is an outdated practice compared to modern `@types/node` imports.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Use modern TypeScript import syntax if the compiler version allows, though this package might not fully support it. The README suggests it compiles to a `require` call anyway.","message":"The `import ... module` syntax for internal module dependencies shown in the README (e.g., `import barmodule = module('barmodule');`) has been deprecated in modern TypeScript in favor of `import * as barmodule from './barmodule';` or default imports.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `require('typescript-require');` is called once at the very beginning of your application's entry point, and verify the path to the `.ts` file.","cause":"The `typescript-require` hook was not initialized before attempting to require a `.ts` file, or the path is incorrect.","error":"Error: Cannot find module 'some-ts-file'"},{"fix":"Check the `typescript-require` configuration options like `targetES5` and `nodeLib`. Review the TypeScript code for errors that might be silently emitted (if `emitOnError: true`) but still lead to broken JS. Consider enabling `exitOnError: true` temporarily to halt execution on compilation failures.","cause":"TypeScript compilation errors occurred, potentially due to outdated `targetES5` settings or missing type definitions, leading to incorrect or incomplete JavaScript output that fails at runtime.","error":"TypeError: Object has no method 'toLowerCase' (or similar runtime type errors)"}],"ecosystem":"npm"}