Fix TypeScript Error: Could not find a declaration file for module ts(7016)

intermediate๐Ÿ”ต TypeScript2026-03-21| TypeScript 4.x/5.x, Node.js projects, React/Next.js/Vue apps, any OS

Error Message

Could not find a declaration file for module 'library-name'. ts(7016)
#typescript#declaration#d-ts#types#definitely-typed

The Error

You import a package and TypeScript immediately throws this:

Could not find a declaration file for module 'library-name'. '/path/to/node_modules/library-name/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/library-name` if it exists or add a new declaration (.d.ts) file containing `declare module 'library-name';`  ts(7016)

Build fails. Editor lights up red. Deploy pipeline blocked.

Why This Happens

TypeScript needs type information to understand what a module exports โ€” its functions, parameters, return types. That information lives in .d.ts declaration files.

When a package ships without them, TypeScript has no idea what the module looks like. It refuses to proceed unless you explicitly allow implicit any. Three situations trigger ts(7016) most often:

  • The package is pure JavaScript with no bundled types
  • The @types/library-name community package isn't installed
  • You're importing a local JS file without a matching .d.ts

Fix 1: Install the @types Package (Most Common)

First, check whether the community has already written types on DefinitelyTyped:

npm install --save-dev @types/library-name
# or with yarn
yarn add -D @types/library-name
# or with pnpm
pnpm add -D @types/library-name

The ones you'll hit constantly:

npm install --save-dev @types/lodash
npm install --save-dev @types/express
npm install --save-dev @types/node
npm install --save-dev @types/react @types/react-dom

After installing, restart your TypeScript server in VS Code: Ctrl+Shift+P โ†’ TypeScript: Restart TS Server. Done.

Verify the package exists first

npm info @types/library-name

Package info returned? Install it. 404-style error? No community types exist โ€” jump to Fix 2.

Fix 2: Write the Declaration Yourself

No types exist anywhere and you need this unblocked now. Create a declaration file in your project:

mkdir -p src/@types

Create src/@types/library-name.d.ts:

declare module 'library-name';

One line. It tells TypeScript: "trust me, this module exists โ€” treat everything from it as any." Workaround, not a fix, but it unsticks you immediately.

Better: type only what you actually use

Know the shape of what you're importing? Be specific. You get real type safety without typing every export:

declare module 'legacy-analytics' {
  interface TrackOptions {
    event: string;
    userId?: string;
    properties?: Record<string, unknown>;
  }

  export function track(options: TrackOptions): void;
  export function identify(userId: string): void;
  export default { track, identify };
}

Fix 3: Point tsconfig.json at Your Declaration Files

TypeScript won't find your .d.ts files automatically. Open tsconfig.json and check typeRoots and include:

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types",
      "./src/@types"
    ]
  },
  "include": [
    "src",
    "src/@types"
  ]
}

Watch out for this trap: if typeRoots is defined but omits ./node_modules/@types, every installed @types package gets silently ignored. That single missing entry causes a lot of head-scratching.

Fix 4: allowJs for Gradual JS-to-TS Migrations

Migrating a JavaScript project and hitting ts(7016) on your own JS files? Add this to tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false
  }
}

TypeScript infers types from plain JS. No explicit declarations needed. Good for incremental adoption.

Fix 5: Quick Escape Hatch (Don't Leave It Here)

Deadline in an hour, types can wait? Disable implicit any checking:

{
  "compilerOptions": {
    "noImplicitAny": false
  }
}

Or suppress a single import:

// @ts-expect-error -- TODO: add types for untyped-library
import legacyLib from 'untyped-library';

Use // @ts-expect-error instead of // @ts-ignore. The difference matters: @ts-expect-error yells at you when the suppression is no longer needed. @ts-ignore stays silent forever.

Verification

Once you've applied a fix, confirm it held:

# Zero errors = you're good
npx tsc --noEmit

# Check specifically for ts(7016)
npx tsc --noEmit 2>&1 | grep 7016

In VS Code, hover over the import. Seeing actual type signatures instead of any? Types loaded correctly.

Prevention

  • Before adding any JS library, search for its @types package โ€” many exist and take 10 seconds to install
  • Many modern packages ship types directly now. Check their package.json for a "types" or "typings" field โ€” no @types package needed
  • Add tsc --noEmit to CI โ€” catches new instances before they reach code review
  • Keep src/@types/ in version control for custom declarations. Don't scatter .d.ts files across the project

Related Error Notes