🔧 Error Fixes
· 1 min read

React: Cannot Find Module or Its Corresponding Type Declarations — How to Fix It


Cannot find module './components/Header' or its corresponding type declarations.
Cannot find module 'react-icons' or its corresponding type declarations.
TS2307: Cannot find module '@/lib/utils' or its corresponding type declarations.

TypeScript can’t find the file or package you’re importing. Different causes depending on whether it’s a local file or an npm package.

Fix 1: Install missing type definitions

For npm packages that don’t include types:

# The package exists but has no types
npm install --save-dev @types/react-icons

# Common ones people forget
npm install --save-dev @types/node
npm install --save-dev @types/react

Not every package has @types/.... Check if it exists:

npm search @types/package-name

Fix 2: Create a type declaration file

If no @types/... exists, create src/types.d.ts:

// For packages without types
declare module 'some-untyped-package';

// For CSS modules
declare module '*.css';
declare module '*.module.css';

// For image imports
declare module '*.png';
declare module '*.svg';
declare module '*.jpg';

Fix 3: Fix path aliases

If you’re using @/ imports:

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
  }
}

After changing tsconfig.json, restart your dev server.

Fix 4: Check the file extension

// ❌ TypeScript might not find .tsx files without config
import Header from './Header';

// ✅ Check that the file is actually Header.tsx (not Header.js)
// And that tsconfig.json includes tsx in "include"

tsconfig.json:

{
  "include": ["src/**/*.ts", "src/**/*.tsx"]
}

Fix 5: Restart the TypeScript server

Sometimes the TS server just needs a kick:

  • VS Code: Cmd+Shift+P → “TypeScript: Restart TS Server”
  • Terminal: stop and restart your dev server
📘