How to Solve cannot find module and its corresponding type declarations

To solve cannot find module and its corresponding type declarations error in JavaScript, install that module and import the library in your program or file. For example, if you are working with react module, and you get an error like: “cannot find module ‘react’ and its corresponding type of declarations”, that means you forgot to install the library in your system.

How to solve cannot find module ‘react’ and its corresponding type declarations

To solve the cannot find module ‘react’ and its corresponding type declarations” error in JavaScript,  install types for the libraries. For example, if you are working with the “react-leaflet” module, you can install the “react-leaflet” module, and then we need to use the following command:

npm install @types/react-leaflet

Similarly, for other libraries, use the below command structure:

npm install @types/your_library_name

Now this will help solve this error. Let’s look at the second solution now.

By Creating a decs.d.ts file

If you create a decs.d.ts file in the root folder of your project with module name declaration, this can lead to the fix of your error. In this case, I am declaring the react-leaflet module in the decs.d.ts file as:

declare module ‘react-leaflet’

Similarly, for other libraries, use the below module structure:

declare module ‘LibraryName’

The decs.d.ts file is used to fix the “cannot find module ‘react’ and its corresponding type declarations” error. Let us look at the 3rd solution to fix the error:

Using @ts-ignore

This is yet another method using which we can fix the mentioned error. By using @ts-ignore, we can fix the error. We can use this as follows:

// @ts-ignore 
import Map from 'react-leaflet'

That’s it for this tutorial.

Related posts

How to Solve cannot read property of null

How to Solve SyntaxError: Unexpected end of input

How to Solve Uncaught ReferenceError: required is not defined

How to solve reference error: window is not defined

How to solve reference error: document is not defined

Leave a Comment