Fixing the 'Could not find a declaration file for module' Error in TypeScript

intermediate⚛️ React2026-06-14| React 18+, TypeScript 4.5+, VS Code, Node.js environment

Error Message

Could not find a declaration file for module 'module-name'. '/path/to/module.js' implicitly has an 'any' type.
#react#typescript#declaration-file#npm

The Late-Night Build Failure

It’s 10 PM. You just finished a feature using a new third-party library. Everything looks perfect in your head, but when you run the build, the terminal explodes with red text. The culprit? A classic TypeScript gatekeeping error:

Could not find a declaration file for module 'module-name'. '/path/to/module.js' implicitly has an 'any' type.

You’ll likely see that frustrating red squiggly line under your import statement in VS Code. TypeScript is essentially saying: "I see this JavaScript code, but I don't know what its objects look like. I'm not going to guess, so I'm stopping here." Here is how to unblock your workflow and get back to shipping code.

The Quick Debugging Checklist

Before trying to force a fix, check these three things to see why TypeScript is complaining:

  • Check the source: Look inside node_modules/module-name. If you don't see an index.d.ts file or a types field in package.json, the library was written in plain JavaScript.
  • Search DefinitelyTyped: Most older JS libraries have community-supported types.
  • Verify your paths: If this is a local file, double-check your baseUrl in tsconfig.json. A typo there can make a local file look like a missing external module.

Solution 1: Use DefinitelyTyped (The Best Fix)

Most of the time, you aren't the first person to use this library with TypeScript. Popular tools like lodash or react-datepicker don't ship with types, but the community maintains them in the @types namespace. This takes 10 seconds to fix.

# Replace 'module-name' with your specific library
npm install --save-dev @types/module-name

# Using yarn or pnpm?
yarn add -D @types/module-name
pnpm add -D @types/module-name

Once the install finishes, the error should vanish. If VS Code is still stubborn, restart the TypeScript server by hitting Ctrl+Shift+P and selecting TypeScript: Restart TS Server.

Solution 2: Create a Custom Declaration File

If you're using an obscure or internal library, an @types package might not exist. In this case, you need to tell TypeScript to stop worrying. You can create a "shorthand" declaration that treats the whole module as an any type. It's not perfectly safe, but it works.

  • Create a types folder inside your src directory.
  • Create a file named declarations.d.ts.
  • Add this single line:
// src/types/declarations.d.ts
declare module 'module-name';

TypeScript usually picks up .d.ts files automatically. If it doesn't, ensure your tsconfig.json includes your types folder:

// tsconfig.json
{
  "include": ["src/**/*", "src/types/*.d.ts"]
}

Solution 3: Handling SVGs and CSS Modules

You’ll often see this error when importing non-code assets. TypeScript expects code, so when you import a .png or .module.css, it gets confused. You can fix this globally in that same declarations.d.ts file:

// Handle CSS Modules
declare module '*.module.css' {
  const classes: { [key: string]: string };
  export default classes;
}

// Handle Image imports
declare module '*.svg' {
  const content: any;
  export default content;
}

Solution 4: The "I Need to Ship Now" Option

If you are under a tight deadline and only a single import is failing, use an escape hatch. This ignores the error for that specific line. Use this sparingly—relying on it too much defeats the point of using TypeScript at all.

// @ts-ignore
import { UntypedComponent } from 'some-old-library';

Verification

Don't just assume it works because the red line is gone. Confirm it with these steps:

  • Run a manual check: Execute npx tsc --noEmit. This tests your entire project for type errors without generating files.
  • Clean your cache: If errors persist, delete your .next or dist folder and restart your dev server. Sometimes old type definitions hide in the cache.

The Bottom Line

Type errors are often a sign that your project has a blind spot. While Solution 2 is the fastest way to unblock your work, it removes the "safety net" for that library. If that library is a core part of your app, consider writing a few interfaces in your .d.ts file. It takes more effort but gives you the autocomplete and protection that makes TypeScript worth using in the first place.

Related Error Notes