{"id":15730,"library":"npm-dts","title":"NPM DTS Single File Generator","description":"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`.","status":"active","version":"1.3.13","language":"javascript","source_language":"en","source_url":"https://github.com/vytenisu/npm-dts","tags":["javascript","NPM","dts","cli","package","ts","TypeScript","tsc","index.d.ts"],"install":[{"cmd":"npm install npm-dts","lang":"bash","label":"npm"},{"cmd":"yarn add npm-dts","lang":"bash","label":"yarn"},{"cmd":"pnpm add npm-dts","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for `npm-dts` to analyze TypeScript source files and generate declaration files from your project.","package":"typescript","optional":false}],"imports":[{"note":"Primarily a CLI tool; the `generate` function allows programmatic use for advanced build workflows. ESM is preferred.","wrong":"const { generate } = require('npm-dts');","symbol":"generate","correct":"import { generate } from 'npm-dts';"}],"quickstart":{"code":"// package.json (inside your library project)\n{\n  \"name\": \"my-library\",\n  \"version\": \"1.0.0\",\n  \"main\": \"dist/index.js\",\n  \"types\": \"dist/index.d.ts\", // Point to the generated declaration file\n  \"devDependencies\": {\n    \"typescript\": \"^5.0.0\", // Required peer dependency for npm-dts\n    \"npm-dts\": \"^1.3.0\"\n  },\n  \"scripts\": {\n    \"build:ts\": \"tsc\", // Standard TypeScript compilation\n    \"build:dts\": \"npm-dts generate --output dist/index.d.ts\", // Generate bundled d.ts\n    \"build\": \"npm run build:ts && npm run build:dts\"\n  },\n  \"files\": [\n    \"dist\"\n  ]\n}\n\n// src/index.ts (example source file)\nexport function greet(name: string): string {\n  return `Hello, ${name}!`\n}\n\nexport interface User {\n  id: number;\n  name: string;\n}\n\nexport class Greeter {\n  constructor(private message: string) {}\n  sayHello(): string {\n    return this.message;\n  }\n}\n\n// To run this after project setup:\n// 1. npm install\n// 2. npm run build\n// This will compile TS to JS, then bundle all .d.ts files into a single dist/index.d.ts.","lang":"typescript","description":"Demonstrates how to integrate `npm-dts` into a `package.json` script to generate a single `index.d.ts` file after TypeScript compilation for a library project."},"warnings":[{"fix":"Ensure `typescript` is listed in your `package.json` under `devDependencies` (e.g., `npm install --save-dev typescript`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Explicitly set `compilerOptions.rootDir` in your `tsconfig.json`, or provide the `--entry` path relative to the inferred `rootDir` (e.g., `src/index.ts` if `src` is your `rootDir`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"It is best practice to resolve all TypeScript compilation errors in your source code before running `npm-dts` for reliable type generation. Use `--force` only when absolutely necessary and carefully inspect the generated output.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Run `npm install --save-dev typescript` in your project's root directory.","cause":"The `typescript` package is not installed as a local dependency in the project where `npm-dts` is being run.","error":"Error: Cannot find module 'typescript'"},{"fix":"Verify 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.","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`.","error":"Error: ENOENT: no such file or directory, stat 'your/path/index.ts'"},{"fix":"Before running `npm-dts`, ensure your project compiles without errors by running `tsc --noEmit`. Address all reported TypeScript errors in your source code.","cause":"There are unhandled compilation errors in your source TypeScript files that `npm-dts` cannot resolve, preventing successful declaration file generation.","error":"TypeScript compilation failed with errors."}],"ecosystem":"npm"}