Dynamically import and compile TypeScript files on the fly in Node.js (>=18). Current stable version is 5.0.0-beta.1, with a release cadence of major and minor updates every few months. It uses native async import (not require), caches compiled JS in node_modules/ts-import/cache, and provides a customizable Compiler class. Key differentiators vs alternatives: it's asynchronous, minimal dependencies (only one internal package with 0 deps), highly configurable with all tsc flags, and does not interfere with native import/require. It has TypeScript type definitions bundled and requires TypeScript 5 as a peer dependency.
npm install ts-importNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Shows how to use the default tsImport compiler and create a custom Compiler instance with different tsc options.
Update code to use async/await. Example: const result = await tsImport.compile('file.ts')Change 'import Compiler from 'ts-import'' to 'import { Compiler } from 'ts-import''Be aware that cache is stored in node_modules. To persist cache, consider using a custom cache directory via Compiler options.
Ensure TypeScript 5 is installed: npm install typescript@5
Install typescript: npm install typescript
Use named import: import { tsImport } from 'ts-import'Set 'type': 'module' in package.json or use Compiler with module: 'commonjs'.
Pass compilerOptions: { allowImportingTsExtensions: true } to the Compiler.