Cannot find module 'X' Error in TypeScript [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
3 min

banner

# Cannot find module 'X' Error in TypeScript

The "Cannot find module or its corresponding type declarations" occurs for multiple reasons:

  • Forgetting to install a third-party package or its type definitions.
  • Having a glitched node_modules directory.
  • Specifying an incorrect path to the module you are importing from.
  • Setting the moduleResolution property to an incorrect value in tsconfig.json.

# Install the third-party module

If the module is a third-party module, make sure you have it installed.

shell
npm install module-name

install third party module in typescript

If the module has type definitions, make sure to install them as well.

shell
npm install --save-dev @types/module-name

install types of package

Make sure to replace module-name with the name of the module in your error message.

# Delete your node_modules and reinstall your dependencies

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.

shell
# 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.

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.

# Make sure your import statements use consistent casing

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.

index.ts
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.

tsconfig.json
{ "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.

When this option is set to 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.

# Set moduleResolution to node in your tsconfig.json file

If that doesn't help or TypeScript can't locate your local modules, try setting moduleResolution to node in your tsconfig.json file.

tsconfig.json
{ "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.

tsconfig.json
{ "compilerOptions": { // ... }, "include": ["src/**/*"], "exclude": ["node_modules", "src/**/*.spec.ts"] }
For example, if the module is out of the 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:

# Conclusion

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev