Last updated: Feb 27, 2024
Reading timeยท3 min
The "Cannot find module or its corresponding type declarations" occurs for multiple reasons:
node_modules
directory.moduleResolution
property to an incorrect value in
tsconfig.json
.If the module is a third-party module, make sure you have it installed.
npm install module-name
If the module has type definitions, make sure to install them as well.
npm install --save-dev @types/module-name
module-name
with the name of the module in your error message.Try removing your node-modules
directory and your
package-lock.json file, re-run
npm install
and reload your IDE.
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, issue the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
Make sure to reload your IDE as VS Code often glitches and needs a reboot.
Some operating systems like Windows treat file and directory names as case-insensitive.
However, macOS and Linux treat file and directory names as case-sensitive.
Make sure you haven't misspelled the path to the module in your import statement.
import { country } from './myFile'; import { country } from './MyFile';
The two import statements do the same on Windows machines because file names on Windows are case-insensitive.
However, if you run this code on a Unix-like operating system like Linux or macOS, you would get an error, because they treat file names case-sensitively and these are 2 completely different files.
A best practice is to use the forceConsistentCasingInFileNames
option in your
tsconfig.json file.
{ "compilerOptions": { "forceConsistentCasingInFileNames": true, // ... } }
When the
forceConsistentCasingInFileNames
option is set to true
, TypeScript will issue an error if you try to include a
file by using a different casing from the casing of the file name on the disk.
For example, if you try to import from MyFile.ts
, but the name of the file is
myFile.ts
, you'd get an error.
false
, TypeScript follows the case sensitivity rules of the file system it's running on.This can be very confusing if some of the developers on your team use Windows
machines and other Unix-like OS, because if you try to import myFile.ts
by
specifying MyFile.ts
, it will be found on Windows, but not on macOS or Linux.
I've also written a detailed guide on how to import values from another file in TS.
moduleResolution
to node
in your tsconfig.json
fileIf that doesn't help or TypeScript can't locate your local modules, try setting
moduleResolution
to node
in your tsconfig.json
file.
{ "compilerOptions": { "moduleResolution": "node", // ๐๏ธ ... rest } }
You can read more about classic
vs node
module resolution in the
TypeScript docs.
If that doesn't help, make sure the module you are trying to import is tracked by TypeScript.
It should be covered in your include
array setting and not be present in the
exclude
array in your tsconfig.json
file.
{ "compilerOptions": { // ... }, "include": ["src/**/*"], "exclude": ["node_modules", "src/**/*.spec.ts"] }
src
directory whilst using the configuration from the code snippet, TypeScript wouldn't be able to find it.Make sure you haven't excluded the module by adding it to your exclude
array.
If your error message changes to "Could not find declaration file for module 'module-name'", then TypeScript has located the module you are trying to import, but can't find its type declarations.
If that is the case, check out my other article - Could not find declaration file for module 'X' Error.
If you get the error when using any of the following packages, click on the relevant article:
The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project.
To solve the error, install the module and try setting moduleResolution
to
node
in your tsconfig.json
file.