TS2307: Cannot find module 'X' or its corresponding type declarations
What’s happening?
Section titled “What’s happening?”This error occurs when TypeScript cannot find the module you’re trying to import.
This can happen due to missing dependencies, incorrect paths, or missing type declarations.
Reproduction
Section titled “Reproduction”import axios from 'axios';// ❌ Cannot find module 'axios' or its corresponding type declarationsWhy does TypeScript complain?
Section titled “Why does TypeScript complain?”TypeScript checks:
- Is the package installed in
node_modules? → ❌ - Does the file exist at the import path? → ❌
- Are type declarations available? → ❌
If all checks fail, the import cannot be resolved.
Solutions
Section titled “Solutions”Option 1: Install the missing package
Section titled “Option 1: Install the missing package”npm install axios# oryarn add axios# orpnpm add axiosWhen to use:
- ✅ The package is not installed
- ✅ Most common fix
Option 2: Install type declarations
Section titled “Option 2: Install type declarations”# For packages without built-in typesnpm install --save-dev @types/nodenpm install --save-dev @types/reactWhen to use:
- ✅ Package is installed but has no TypeScript types
- ✅ Types are in DefinitelyTyped (@types/* packages)
Option 3: Fix the import path
Section titled “Option 3: Fix the import path”// ❌ Wrongimport { helper } from './utils';
// ✅ Correctimport { helper } from './utils.ts';// orimport { helper } from './utils/index.ts';When to use:
- ✅ The file exists but path is incorrect
- ✅ Missing file extension in Deno/some bundlers
Option 4: Configure module resolution in tsconfig.json
Section titled “Option 4: Configure module resolution in tsconfig.json”{ "compilerOptions": { "moduleResolution": "node", "baseUrl": ".", "paths": { "@/*": ["src/*"] } }}When to use:
- ✅ Using path aliases
- ✅ Custom module resolution strategy
Option 5: Create a type declaration file (for untyped packages)
Section titled “Option 5: Create a type declaration file (for untyped packages)”declare module 'untyped-package' { export function doSomething(): void;}When to use:
- ✅ Package has no types and no @types/* package available
- ⚠️ You’ll need to maintain these types yourself
Common scenarios
Section titled “Common scenarios”Missing Node.js types
Section titled “Missing Node.js types”import * as fs from 'fs';// ❌ Cannot find module 'fs'
// ✅ Fix: Install Node typesnpm install --save-dev @types/nodeWrong file extension
Section titled “Wrong file extension”import { api } from './api.js';// ❌ File exists as api.ts, not api.js
// ✅ Fix: Use correct extension or omit itimport { api } from './api';Importing JSON files
Section titled “Importing JSON files”import data from './data.json';// ❌ Cannot find module './data.json'
// ✅ Fix: Enable resolveJsonModule in tsconfig.json{ "compilerOptions": { "resolveJsonModule": true }}Path alias not configured
Section titled “Path alias not configured”import { Button } from '@components/Button';// ❌ Cannot find module '@components/Button'
// ✅ Fix: Configure paths in tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@components/*": ["src/components/*"] } }}CSS/Image imports without declarations
Section titled “CSS/Image imports without declarations”import styles from './App.module.css';// ❌ Cannot find module './App.module.css'
// ✅ Fix: Create declaration file// src/types/modules.d.tsdeclare module '*.module.css' { const classes: { [key: string]: string }; export default classes;}
declare module '*.png' { const value: string; export default value;}Key takeaways
Section titled “Key takeaways”- Always install packages before importing them
- Install
@types/*packages for type declarations - Check import paths are correct
- Configure
tsconfig.jsonfor custom module resolution - Create
.d.tsfiles for untyped modules