Registry / devops / typescript-require

typescript-require

JSON →
library0.3.0jsnpmunverified

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`.

npm install typescript-require
INSTALL
IMPORT
SIG · TYPESCRIPT-REQUIRE
T
typescript-require
devopsjavascriptv0.3.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TypeScriptRequire
require('typescript-require');
import 'typescript-require';
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.
ConfiguredTypeScriptRequire
require('typescript-require')({ targetES5: true, exitOnError: false });
import typescriptRequire from 'typescript-require';
Configuration is passed directly as an argument to the initial `require` call, not via a separate import or method.
TSModuleImport
var myModule = require('./myModule.ts');
import { myModule } from './myModule.ts';
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.

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.

/* app.js */ const path = require('path'); const fs = require('fs'); // Create a temporary directory for output if it doesn't exist const tmpDir = path.join(__dirname, 'tmp'); if (!fs.existsSync(tmpDir)) { fs.mkdirSync(tmpDir); } // Initialize typescript-require with custom options require('typescript-require')({ targetES5: true, exitOnError: false, // Don't exit process immediately on error for demonstration tmpDir: tmpDir // Specify temporary directory }); // Create a sample TypeScript file dynamically for demonstration const tsContent = ` export function lowercase(val: string): string { return val.toLowerCase(); } export function uppercase(val: string): string { return val.toUpperCase(); } `; const funcsPath = path.join(__dirname, 'funcs.ts'); fs.writeFileSync(funcsPath, tsContent); // Require the TypeScript module const funcs = require(funcsPath); console.log(funcs.lowercase("HELLO FROM TYPESCRIPT!")); console.log(funcs.uppercase("hello from typescript!")); // Clean up generated files and directory fs.unlinkSync(funcsPath); // Optional: Clean up compiled JS files and source maps in tmpDir as well fs.readdirSync(tmpDir).forEach(file => { if (file.startsWith('funcs.') && (file.endsWith('.js') || file.endsWith('.map'))) { fs.unlinkSync(path.join(tmpDir, file)); } }); fs.rmdirSync(tmpDir);
Debug
Known issues
breakingThis 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.
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`.
affects: All versions, especially with TypeScript >= 2.0
gotchaThe `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.
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.
affects: All versions
deprecatedThe `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.
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.
affects: All versions
Errors
Common errors & fixes
Error: Cannot find module 'some-ts-file'
The `typescript-require` hook was not initialized before attempting to require a `.ts` file, or the path is incorrect.
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.
TypeError: Object has no method 'toLowerCase' (or similar runtime type errors)
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.
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.
Upgrade
Version history
0.3.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
typescript-require — npm install typescript-require · libregistry