DEV Community

Cover image for Fix TS7016: Could Not Find Declaration File for Module
Mahdi BEN RHOUMA
Mahdi BEN RHOUMA

Posted on • Originally published at iloveblogs.blog

Fix TS7016: Could Not Find Declaration File for Module

You import a perfectly normal npm package and TypeScript stops you:

Could not find a declaration file for module 'some-lib'.
'.../node_modules/some-lib/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/some-lib` if it exists or add a new
  declaration (.d.ts) file containing `declare module 'some-lib';`
Enter fullscreen mode Exit fullscreen mode

This is TS7016. TypeScript even prints its own two suggested fixes. This guide explains when each is right, plus the case the message doesn't mention: the package may already ship types.

{ name: "TypeScript", version: "strict / noImplicitAny" },
{ name: "DefinitelyTyped", version: "@types" },
]} />

Why it happens

TS7016 fires when all three are true:

  1. You import a module that resolves to a plain JavaScript file with no bundled .d.ts, and
  2. there is no @types/X package installed, and
  3. noImplicitAny is on (it is, under strict).

Without type information the module would silently become any. The noImplicitAny docs put it plainly: with the flag on, "TypeScript will issue an error whenever it would have inferred any." So TS7016 is the protection working — not a broken install.

Fix 1 (preferred) — Install the DefinitelyTyped types

npm install --save-dev @types/some-lib
Enter fullscreen mode Exit fullscreen mode

How to know it exists: DefinitelyTyped publishes type packages to npm under the @types scope with the same name as the underlying package (react@types/react). Per the handbook, "TypeScript automatically finds type definitions under node_modules/@types, so there's no other step needed." If the install errors with 404, no community types exist — go to Fix 3.

Fix 2 — Check if the package already ships types

Sometimes TS7016 is misdiagnosed: the package does bundle types and you're chasing a phantom. Look in the dependency's package.json for a types (or its synonym typings) field:

{
  "main": "./lib/main.js",
  "types": "./lib/main.d.ts"
}
Enter fullscreen mode Exit fullscreen mode

If a package bundles types but you still see TS7016, suspect a stale install, a typesVersions mismatch for your TS version, or an exports map that doesn't expose the .d.ts under your moduleResolution. The fix there is reinstalling or adjusting module resolution — not adding @types.

Fix 3 — Write a local declaration

When neither @types/X nor bundled types exist, declare the module yourself. Create a file like src/types/some-lib.d.ts. The minimal "make it any" form (TypeScript's own second suggestion):

// src/types/some-lib.d.ts
declare module 'some-lib';
Enter fullscreen mode Exit fullscreen mode

Better — a typed stub keeps real safety instead of opting the whole module into any:

declare module 'some-lib' {
  export function doThing(input: string): string;
  export interface Options {
    retries?: number;
    timeout?: number;
  }
  export const version: string;
}
Enter fullscreen mode Exit fullscreen mode


The .d.ts must be inside your tsconfig include scope, or TypeScript won't pick it up. If it's ignored, check include/files in tsconfig.

Last resort — turn off the check

// silences ALL of these, project-wide
{ "compilerOptions": { "noImplicitAny": false } }
Enter fullscreen mode Exit fullscreen mode

Or // @ts-ignore on the import line. Both work and both are bad:

wrong="// @ts-ignore — kills checking for this import and breaks silently when the lib changes"
right="npm i -D @types/some-lib — or a typed declare module stub: error gone, safety kept"
/>

  • The package ships ESM-only types but your moduleResolution is node16/nodenext/bundler and its exports map hides the .d.ts — that's a resolution problem, not a missing-types one.
  • Your @types/X version drifted from the runtime package version — reinstall to match, or you'll get wrong types rather than missing ones.

Decision guide

Check Result Do this
npm i -D @types/X succeeds Community types exist Done
Package's package.json has types/typings It ships its own Reinstall / fix resolution
Neither exists Truly untyped declare module 'X'; (+ a typed stub)
Nothing else and you're in a hurry @ts-ignore (last resort)

Official references: Consuming declaration files, Type declarations, module .d.ts template, noImplicitAny.

Related Articles

Frequently Asked Questions

How do I fix "Could not find a declaration file for module"?

Run npm install --save-dev @types/X first — most popular packages have community types on DefinitelyTyped, auto-discovered in node_modules/@types. If none exist, write a local declare module 'X'; inside your tsconfig include scope.

Why does this error only appear with strict mode?

It's gated by noImplicitAny, which strict enables. Without it an untyped module silently becomes any; with it, TypeScript surfaces TS7016 instead. That's the protection working.

Should I just use @ts-ignore to silence it?

Prefer @types or a declaration. @ts-ignore suppresses the one error but discards type checking for that import and breaks across refactors. Disabling noImplicitAny removes the protection project-wide. Both are last resorts.


Originally published at https://www.iloveblogs.blog

Top comments (0)