NPM DTS Single File Generator
npm-dts is a utility designed to consolidate all TypeScript declaration files (.d.ts) generated for an NPM package into a single index.d.ts file. This enables library developers to distribute bundled JavaScript code while providing comprehensive type definitions, ensuring full type-checking and IDE intellisense for consumers without exposing the original TypeScript source. The current stable version is 1.3.13. While a specific release cadence isn't published, the incremental versioning suggests regular, feature-driven updates. Its primary differentiator is simplifying the distribution of type definitions for compiled libraries, acting as a post-processing step for `tsc` output to create a unified declaration bundle, making library consumption straightforward for TypeScript users. It operates primarily via a command-line interface, requiring `typescript` to be installed in the consuming project's `node_modules`.
Common errors
-
Error: Cannot find module 'typescript'
cause The `typescript` package is not installed as a local dependency in the project where `npm-dts` is being run.fixRun `npm install --save-dev typescript` in your project's root directory. -
Error: ENOENT: no such file or directory, stat 'your/path/index.ts'
cause The entry file specified by `--entry` (or the default `index.ts`) does not exist at the expected path relative to the `tsconfig.json`'s `rootDir`.fixVerify the path provided to `--entry` is correct and relative to your TypeScript `rootDir`. Check your `tsconfig.json` for `compilerOptions.rootDir` and ensure the entry file exists. -
TypeScript compilation failed with errors.
cause There are unhandled compilation errors in your source TypeScript files that `npm-dts` cannot resolve, preventing successful declaration file generation.fixBefore running `npm-dts`, ensure your project compiles without errors by running `tsc --noEmit`. Address all reported TypeScript errors in your source code.
Warnings
- gotcha It is critical that `typescript` is installed as a local `devDependency` or `dependency` in the project where `npm-dts` is executed. `npm-dts` relies on the local `tsc` binary and its associated modules.
- gotcha The `--entry` option's path resolution can be complex when `rootDir` is not explicitly set in `tsconfig.json`. TypeScript might infer `rootDir`, leading to unexpected 'file not found' errors if the `--entry` path doesn't align with the inference.
- gotcha Using the `--force` option can result in a partially generated or incorrect declaration file if underlying TypeScript compilation errors are significant. It forces generation despite non-critical errors.
Install
-
npm install npm-dts -
yarn add npm-dts -
pnpm add npm-dts
Imports
- generate
const { generate } = require('npm-dts');import { generate } from 'npm-dts';
Quickstart
// package.json (inside your library project)
{
"name": "my-library",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts", // Point to the generated declaration file
"devDependencies": {
"typescript": "^5.0.0", // Required peer dependency for npm-dts
"npm-dts": "^1.3.0"
},
"scripts": {
"build:ts": "tsc", // Standard TypeScript compilation
"build:dts": "npm-dts generate --output dist/index.d.ts", // Generate bundled d.ts
"build": "npm run build:ts && npm run build:dts"
},
"files": [
"dist"
]
}
// src/index.ts (example source file)
export function greet(name: string): string {
return `Hello, ${name}!`
}
export interface User {
id: number;
name: string;
}
export class Greeter {
constructor(private message: string) {}
sayHello(): string {
return this.message;
}
}
// To run this after project setup:
// 1. npm install
// 2. npm run build
// This will compile TS to JS, then bundle all .d.ts files into a single dist/index.d.ts.