Lightweight, fast utility that resolves TypeScript import paths to absolute file paths without TypeScript compiler.
npm install ts-import-resolver
- Resolves TypeScript imports to their absolute file paths
- Supports path aliases, baseUrl, and other module resolution options defined in tsconfig.json
- Lightweight and efficient with zero dependencies
- Blazing fast resolution without TypeScript compiler overhead
import { resolveTsImportPath } from 'ts-import-resolver';
import fs from 'fs';
import path from 'path';
// Load your tsconfig.json
const tsconfig = JSON.parse(fs.readFileSync('./tsconfig.json', 'utf8'));
const cwd = path.resolve('.');
const absolutePath = resolveTsImportPath({
path: '@/components/Button', // The import path to resolve
importer: '/path/to/your/file.ts', // The file containing the import
tsconfig, // Your parsed tsconfig object
cwd, // Project root directory
});
if (absolutePath) {
console.log(`Resolved to: ${absolutePath}`);
} else {
console.log('Could not resolve the import');
}
Resolves a TypeScript import path to its absolute file path.
interface resolveTsImportPathOptions {
/**
* The import path to resolve (e.g., './utils', '@/components', etc.)
*/
path: string;
/**
* The absolute path of the file containing the import
*/
importer: string;
/**
* The parsed TypeScript configuration object if available
*/
tsconfig: Record<string, unknown> | null | undefined;
/**
* The custom root of the project
*/
cwd: string;
}
string | null
- The absolute path to the imported module if found, ornull
if it couldn't be resolved.
MIT