How to Fix TypeScript Cannot Find Module Error

Learn what causes the TypeScript 'cannot find module' error and how to fix it with simple steps.

When working with TypeScript, you might see an error message like "Cannot find module 'module-name'". This error happens when TypeScript cannot locate the file or package you are trying to import into your code.

This issue often occurs because the module does not exist at the specified path, the path is misspelled, TypeScript is missing type declarations, or your project configuration is incorrect. Fixing it usually means checking the module location, installing missing packages, or updating your TypeScript settings.

typescript
import { greet } from './utils';

console.log(greet('World'));

In this example, if the file './utils.ts' is missing or the path is wrong, TypeScript will show the 'cannot find module' error. To fix this, make sure './utils.ts' exists and the path is correct. If importing a third-party package, run 'npm install package-name' to add it to your project. Also, you can add a 'types' folder or configure the 'typeRoots' in your tsconfig.json if TypeScript can't find type definitions.