TypeScript Declaration File Resolver
dts-resolver is a utility library designed to accurately locate and resolve TypeScript declaration files (.d.ts) for JavaScript and TypeScript dependencies within a project. It leverages the high-performance `oxc-resolver` engine to efficiently navigate `node_modules` and package `exports` maps, providing correct paths to type definitions. The current stable version is 2.1.3. The package has a moderately active release cadence, frequently incorporating bug fixes and performance improvements, and keeping up with its underlying resolver. Its key differentiator is its reliance on `oxc-resolver`, offering a robust and modern approach to declaration file resolution, crucial for build tools, IDEs, and other development workflows that need to understand the type landscape of a project's dependencies.
Common errors
-
Error: Cannot find module 'oxc-resolver' from '<your-project-path>'
cause `oxc-resolver` is a peer dependency of `dts-resolver` and must be installed explicitly in your project.fixInstall `oxc-resolver`: `npm install oxc-resolver` or `yarn add oxc-resolver`. -
TypeError: createResolver is not a function (or similar CommonJS error)
cause `dts-resolver` is distributed as an ESM-first package, and attempts to use `require()` directly in a CommonJS context without proper transpilation will fail.fixEnsure your project uses ESM imports (`import ... from '...'`) or is properly configured to transpile ESM to CommonJS if you are in a legacy CommonJS environment. Update your `package.json` to include `"type": "module"` for ESM support.
Warnings
- breaking Version 2.0.0 introduced `oxc-resolver` as a mandatory peer dependency. Projects must explicitly install `oxc-resolver` alongside `dts-resolver`.
- gotcha The `cwd` option in `createResolver` is crucial for correct path resolution, especially in monorepos or projects with non-standard directory structures. Ensure it points to the root of the project where `node_modules` is expected.
Install
-
npm install dts-resolver -
yarn add dts-resolver -
pnpm add dts-resolver
Imports
- createResolver
const { createResolver } = require('dts-resolver');import { createResolver } from 'dts-resolver'; - ResolverOptions
import type { ResolverOptions } from 'dts-resolver';
Quickstart
import { createResolver } from 'dts-resolver';
import path from 'node:path';
async function resolveTypesForPackage(packageName: string) {
const resolver = createResolver({
cwd: process.cwd(), // Define the current working directory for resolution
// You can add custom conditions if your project uses them, e.g., ['node', 'require', 'types']
// conditions: ['node', 'require', 'types'],
// Optionally disable resolving node modules if you only want local files, though usually not desired for dts resolution
// resolveNodeModules: true,
});
try {
const declarationPath = resolver(packageName);
if (declarationPath) {
console.log(`Resolved declaration for '${packageName}': ${declarationPath}`);
console.log(`Located in directory: ${path.dirname(declarationPath)}`);
} else {
console.log(`Could not resolve declaration for '${packageName}'.`);
}
} catch (error) {
console.error(`Error resolving '${packageName}':`, error);
}
}
// Example usage for a common package
resolveTypesForPackage('fast-glob');
// Example for a package that might not have direct types or different structure
resolveTypesForPackage('lodash');