Special Sponsor:PromptBuilder— Fast, consistent prompt creation powered by 1,000+ expert templates.
Make your Product visible here.Contact Us

Home/TypeScript Errors/TS2307

TS2307

Cannot find module 'X' or its corresponding type declarations

TypeScript TS2307 fires when an import cannot be resolved — the module doesn't exist, isn't installed, lacks type declarations, or the path is wrong. It blocks compilation until the module is found.

Why This Happens

Most common causes: the package isn't installed (npm install), the @types package is missing for a JavaScript library, path aliases aren't configured in tsconfig, or you're using a file extension TypeScript doesn't resolve by default.

Code That Triggers TS2307

// TS2307 examples

import lodash from 'lodash';
// Error: Cannot find module 'lodash' or its corresponding type declarations
// Fix: npm install lodash && npm install --save-dev @types/lodash

import config from './config.json';
// Error: Cannot find module './config.json'
// Fix: add "resolveJsonModule": true to tsconfig.json

import { utils } from '@/utils';
// Error: Cannot find module '@/utils'
// Fix: configure path alias in tsconfig.json

How to Fix TS2307

Option 1: Install the package and its types

npm install lodash
npm install --save-dev @types/lodash

Option 2: Configure path aliases in tsconfig.json

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

Option 3: Enable JSON module resolution

// tsconfig.json
{
  "compilerOptions": {
    "resolveJsonModule": true
  }
}

Option 4: Declare an untyped module manually

// types/declarations.d.ts
declare module 'some-untyped-library' {
  const value: any;
  export default value;
}

Frequently Asked Questions — TS2307

What does TS2307 mean?

TS2307 means TypeScript can't find the module you're trying to import. Either the package isn't installed, it has no TypeScript types, the path is wrong, or tsconfig isn't configured to resolve it.

How do I fix TS2307 for an npm package?

First install the package: npm install package-name. If it's a JavaScript-only package, also install types: npm install --save-dev @types/package-name. If no @types exists, declare the module in a .d.ts file.

TS2307 for a local file — how to fix?

Check the relative path is correct. If you're using path aliases like @/ make sure they're configured in both tsconfig.json paths and your bundler (webpack, vite, etc.).

TS2307 for .svg or .png files — how to fix?

Declare the file type in a .d.ts file: declare module '*.svg' { const content: string; export default content; }. Same for .png, .css modules, etc.

TS2307 after upgrading TypeScript — why?

Newer TypeScript versions enforce stricter module resolution. Check moduleResolution in tsconfig — switching to 'bundler' or 'node16' often resolves this after version upgrades.

Convert JavaScript to TypeScript automatically

Paste your JS code and get type-annotated TypeScript — including fixes for common type errors — in seconds.

Try the converter →

Other TypeScript Errors

From the blog

View all →