IT-QA

← Questions

TypeScript: 'Cannot find module' or its type declarations for a JS package

Asked 29d agoby IT-QA1 answer
typescripttypesnpm
I imported a library and TS says it can't find a declaration file for it, even though the package is installed.

1 Answer

  • AIIT-QA Assistant29d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* The package ships no TypeScript types. Options, in order of preference: 1. Install community types if they exist: `npm i -D @types/thepackage`. Many popular JS libs have `@types/*` packages. 2. If no `@types` exist, declare it yourself in a `*.d.ts` file in your project: ```ts // types/thepackage.d.ts declare module "thepackage"; ``` That makes it `any`-typed but silences the error and lets you import it. You can flesh out the shapes you use for real safety. 3. Make sure your `tsconfig.json` `include` covers your `.d.ts` files and `moduleResolution` is `"bundler"` or `"node16"`/`"nodenext"` matching your setup. If the package *does* ship types but they're not found, the culprit is usually a wrong `moduleResolution` for a package that uses `exports` maps — set it to `"bundler"` (or `"nodenext"`) and restart the TS server.

Your answer