What this error means and why it happens

The error "Could not find a declaration file for module" appears when your TypeScript compiler or bundler cannot locate the type definitions for a package you are trying to use. Type definitions tell TypeScript what functions, objects, and properties exist in a module — without them, TypeScript cannot check whether you are using the module correctly.

This error most often occurs when you install a package that does not include its own type definitions and you have not installed a separate types package. It can also happen if the types package exists but is not installed, or if your project configuration is pointing to the wrong location for type files.

The error does not mean your code will fail at runtime — JavaScript does not require type definitions. It means TypeScript will refuse to compile your project until you either provide the type definitions or tell TypeScript to stop checking that particular module.

Key Takeaways

  • Most popular packages include type definitions built-in, but older or less common packages require a separate @types package installed from npm.
  • The fastest fix is usually to install the types package: npm install --save-dev @types/package-name, replacing package-name with the actual module name.
  • If no types package exists, you can tell TypeScript to skip type checking for that module by setting "skipLibCheck": true in your tsconfig.json file.
  • Check your tsconfig.json to ensure the typeRoots and types settings point to the correct directories where type files are stored.

Check whether a types package exists for your module

Before installing anything, confirm that a types package is actually available. Visit the TypeScript website's type search at typesearch.org or search npm directly by typing @types/package-name into the npm search bar. If the package is popular — React, Express, Lodash — a types package almost certainly exists.

If you find the types package listed, note its exact name. Some packages have hyphens or unusual naming. For example, the types for react-dom are installed as @types/react-dom, not @types/react.

If no types package exists, you have two choices: install a types package from a third party (less common and less reliable), or configure TypeScript to skip type checking for that module.

Install the types package from npm

Once you have confirmed the types package exists, install it using npm. Open your terminal in your project directory and run:

npm install --save-dev @types/package-name

Replace package-name with the actual name of the module. For example, if you are using the lodash package, you would run npm install --save-dev @types/lodash. The --save-dev flag installs it as a development dependency, which is correct because type definitions are only needed during development and compilation.

After installation completes, try compiling your TypeScript project again. The error should disappear. If it does not, move to the next section to check your configuration.

Verify your tsconfig.json settings

Your tsconfig.json file controls where TypeScript looks for type definitions. Open the file in your project root and check the typeRoots and types settings.

By default, TypeScript searches in node_modules/@types. If you have customized typeRoots, make sure it includes the directory where your types packages are installed. A typical typeRoots setting looks like this:

"typeRoots": ["./node_modules/@types"]

If you have set a types array, it explicitly lists which type packages to load. If this array exists and does not include your package, TypeScript will ignore the types even if they are installed. Either remove the types array entirely (to load all packages from typeRoots) or add your package name to the list.

After making changes to tsconfig.json, save the file and recompile. You may need to restart your IDE or language server for the changes to take effect.

Skip type checking for modules without types packages

If no types package exists and you cannot find a third-party alternative, you can tell TypeScript to stop checking that module. Add this setting to your tsconfig.json:

"skipLibCheck": true

This tells TypeScript to skip type checking for all declaration files, including ones that are missing or incomplete. This is a project-wide setting, so it affects all modules, not just the one causing the error.

Alternatively, if you want to skip checking only for a specific module, you can use a TypeScript comment in your code:

// @ts-ignoreimport myModule from 'module-without-types';

The @ts-ignore comment tells TypeScript to skip type checking for that single line. Use this sparingly, because it prevents TypeScript from catching real errors in that import.

Check for package.json and installation issues

Sometimes the types package is listed in your package.json but was never actually installed. This can happen if the installation was interrupted or if you are working with code that was cloned from another machine.

Run npm install in your project directory to install all dependencies listed in package.json. Then check that the node_modules/@types directory contains a folder matching your package name.

If you are using a monorepo or a non-standard project structure, the types package may be installed in a different location. Check your package manager's documentation — npm, yarn, and pnpm handle dependency resolution differently.

Verify the module is actually installed

The error can also occur if the main package itself is not installed, even though you are trying to import it. Check your package.json to confirm the package is listed under dependencies or devDependencies.

If the package is listed but you are still seeing the error, run npm install again. If the package is not listed, install it first:

npm install package-name

Then install the types package as described above. Some packages include their own type definitions, so installing the main package may be enough — but if the error persists, the types package is missing.

Frequently Asked Questions

Do I need to install types for every package I use?

No. Modern packages, especially popular ones, often include type definitions built-in. You only need a separate types package if TypeScript reports the error. If your code compiles without errors, the types are already available.

What is the difference between @types packages and built-in types?

Some packages ship with their own type definitions included in the package itself. Others do not include types, so the TypeScript community maintains separate @types packages on npm. Built-in types are always preferred because they are maintained by the package author and stay in sync with the code.

Will my code run if I ignore this error?

Yes. Type definitions only matter to TypeScript during compilation. If you skip type checking or ignore the error, your code will still run in JavaScript. However, you lose TypeScript's ability to catch mistakes before runtime.

Can I create my own type definitions if none exist?

Yes, but it requires understanding TypeScript's declaration syntax. For a quick fix, use skipLibCheck or @ts-ignore. For a permanent solution, you can create a .d.ts file in your project that declares the module's types, though this is time-consuming and error-prone.

Why does the error appear in one project but not another?

Different projects have different tsconfig.json settings and different installed dependencies. A stricter tsconfig.json will catch missing types that a looser one ignores. Also, if the types package is not listed in package.json, it may be installed in one project but not another.