Fix - Could not find declaration file for module 'X' Error


Borislav Hadzhiev

Fri Feb 18 20222 min read


Photo by Jeremy Bishop

Fix - Could not find declaration file for module 'X' Error #

The error "Could not find declaration file for module X" occurs when TypeScript cannot find the type declaration for a module. To solve the error, install the types for the module by running the command from the error message, e.g. npm install -D @types/module-name .

could not find a declaration file for module

The error message shows the command that you should try to run when you get the error. The command in the screenshot above is:

npm i --save-dev @types/uuid

Where uuid is the name of the package that caused the error in the example.

If you are getting the error when trying to import JavaScript files in your project, you have to set allowJs to true in your tsconfig.json file. (In that case scroll down to the tsconfig.json section)

Usually the names of type packages are consistent - @types/module-name, but you could try to look for a definition package by using the official TypeScript type search.

If the error persists, try changing the import statement for the specific package to require().

// 👇️ for default export const yourModule = require('your-module') // 👇️ for named export const { yourModule } = require('your-module');

If the error persists, the third-party module you are importing might not provide typings, which means that you need to declare the module in a file with a .d.ts extension.

TypeScript looks for .d.ts files in the same places it looks for your regular .ts files, which is determined by the include and exclude settings in your tsconfig.json file.

Create a new file named module-name.d.ts next to your regular TypeScript files and add the following line in it.

declare module 'module-name';

Make sure to replace module-name with the name of the module that's causing the error.

This will set the type of the package to any when importing it.

If you want to add your own type definitions for the package, replace the line with the following code:

declare module 'module-name' { export function myFunction(): string; }

The example shows how to add types for a function named myFunction that returns a string.

You only need to add type definitions for the functions you intend to import in your code.

If you are getting the "Could not find a declaration file for module 'X'" error when trying to import JavaScript files into your TypeScript files, you have to set the allowJs option to true in your tsconfig.json file.

{ "compilerOptions": { "allowJs": true, "checkJs": false, // ... your other options }, }

When the allowJs option is enabled, you are allowed to import files with .js and .jsx extension in your TypeScript files. By default you can only import files with .ts and .tsx extensions.

The checkJs option determines if type errors should be reported in JavaScript files.

If this still doesn't work, you should try to ignore the error by adding the following lines above the import.

// eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore import { myFunction } from 'module-name';

Adding these lines above the import statement will suppress the error message.

Use the search field on my Home Page to filter through my more than 1,000 articles.