ts-import-resolver

raw JSON →
0.1.23 verified Fri May 01 auth: no javascript

ts-import-resolver is a lightweight, zero-dependency utility for resolving TypeScript import paths to absolute file paths without invoking the TypeScript compiler. It supports path aliases, baseUrl, and other tsconfig resolution options. Current stable version is 0.1.23, released with minor updates. It is faster than using the TypeScript compiler API for pure path resolution, but lacks full compiler-level features like type checking or extension resolution in some edge cases. Designed for use in tools like bundlers, linters, or custom build scripts that need quick module resolution.

error Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
cause Using CommonJS require() on an ESM-only package.
fix
Replace require('ts-import-resolver') with dynamic import('ts-import-resolver') or set 'type': 'module' in package.json.
error TypeError: tsconfig is not iterable
cause Passing a non-object or undefined tsconfig.
fix
Ensure tsconfig is a valid parsed JSON object from tsconfig.json.
error resolveTsImportPath(...) returned null for path alias
cause Path alias not defined in tsconfig paths, or baseUrl missing.
fix
Verify tsconfig includes the correct 'paths' mapping and 'baseUrl' property.
breaking Package is ESM-only; CommonJS require() will fail.
fix Use ES module imports or dynamic import() in CommonJS projects.
gotcha If tsconfig is null or undefined, resolution falls back to a simple path join; path aliases will not be resolved.
fix Always provide a valid tsconfig object for proper alias resolution.
deprecated The 'cwd' parameter should be an absolute path; relative paths may produce unexpected results.
fix Use path.resolve() to provide an absolute cwd.
gotcha Resolution does not handle extension search with .js extensions in the import path; it resolves based on tsconfig extensions.
fix If import path uses .js extension, ensure tsconfig includes 'allowJs' or handle extension manually.
npm install ts-import-resolver
yarn add ts-import-resolver
pnpm add ts-import-resolver

Shows how to parse tsconfig.json and call resolveTsImportPath to resolve a path alias.

import { resolveTsImportPath } from 'ts-import-resolver';
import fs from 'fs';
import path from 'path';

const tsconfig = JSON.parse(fs.readFileSync('./tsconfig.json', 'utf8'));
const cwd = path.resolve('.');

const absolutePath = resolveTsImportPath({
  path: '@/components/Button',
  importer: '/path/to/your/file.ts',
  tsconfig,
  cwd,
});

if (absolutePath) {
  console.log(`Resolved to: ${absolutePath}`);
} else {
  console.log('Could not resolve the import');
}