TypeScriptError Fix2026 Updated

TypeScript Cannot Find Module— Fixed [Complete Guide 2026]

Also covers: TS2307 · cannot find type declarations · path aliases · moduleResolution

January 20267 min readTypeScript · tsconfig · Node.js

⚡ The Errors You Are Seeing

Error
error TS2307: Cannot find module 'lodash' or its corresponding type declarations.

error TS2307: Cannot find module '@/components/Button' or its corresponding type declarations.

error TS2307: Cannot find module './utils' or its corresponding type declarations.

What Causes TS2307 Cannot Find Module

TS2307 fires when TypeScript's module resolver cannot locate a module you are importing. There are four distinct root causes — each with a different fix:

1

Missing @types Package

Cannot find type declarations for 'lodash', 'express', etc.
3 min

Many npm packages ship JavaScript only, without TypeScript types. The DefinitelyTyped project provides @types/* packages that add types for these.

terminal — install missing @types
# Install type declarations for common packages
npm install -D @types/node
npm install -D @types/lodash
npm install -D @types/express
npm install -D @types/react @types/react-dom

# Check if @types exists for your package
npm info @types/your-package-name

# If @types does NOT exist, create a declaration file (see Fix 4)
tsconfig.json — ensure @types are included
{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types", "./src/types"],
    "types": ["node"] // explicitly include node types if needed
  }
}

Modern packages (React 18+, Prisma, Next.js) ship their own types and do not need a separate @types install. Only legacy packages need @types.

2

Path Aliases Not Configured

@/ aliases · import from '@/components/Button'
4 min

Path aliases require configuration in two places: tsconfig.json (for TypeScript to understand) AND your bundler config (for the runtime to resolve them).

tsconfig.json — define paths
{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"],
      "@components/*": ["./src/components/*"],
      "@lib/*": ["./src/lib/*"],
      "@types/*": ["./src/types/*"]
    }
  }
}
vite.config.ts — mirror aliases for Vite
import { defineConfig } from "vite";
import path from "path";

export default defineConfig({
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
      "@components": path.resolve(__dirname, "./src/components"),
      "@lib": path.resolve(__dirname, "./src/lib"),
    },
  },
});
next.config.js — Next.js reads tsconfig paths automatically
// Next.js 13+ reads tsconfig.json paths natively — no extra config needed
// Just set paths in tsconfig.json and Next.js will resolve them

// If using webpack directly, add to next.config.js:
module.exports = {
  webpack(config) {
    config.resolve.alias["@"] = path.resolve(__dirname, "src");
    return config;
  },
};
3

moduleResolution Set Incorrectly

Cannot find module for packages with exports field
3 min

Modern packages use the exports field in package.json for subpath exports. The legacy node moduleResolution does not support this — upgrade to bundler or node16.

tsconfig.json — correct moduleResolution settings
// For Next.js 14+ and Vite projects:
{
  "compilerOptions": {
    "moduleResolution": "bundler",
    "module": "ESNext",
    "target": "ES2017"
  }
}

// For Node.js with native ESM (package.json type: "module"):
{
  "compilerOptions": {
    "moduleResolution": "node16",
    "module": "node16"
  }
}

// For traditional Node.js (CommonJS):
{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "commonjs"
  }
}

If you switch moduleResolution from 'node' to 'bundler', you may see new errors in other files. Fix each one — the new setting is more correct and will catch real bugs.

4

Missing Declaration File (.d.ts)

Package with no @types — write your own declarations
3 min

When a package has no @types equivalent, create a local declaration file to silence the error.

src/types/vendor.d.ts — minimal declaration
// Minimal — treats the whole module as 'any'
declare module "untyped-package";

// Better — declare the actual exports you use
declare module "untyped-package" {
  export function doThing(input: string): Promise<void>;
  export interface Config {
    timeout: number;
    retries: number;
  }
  export default function main(config: Config): void;
}
tsconfig.json — include your types directory
{
  "compilerOptions": {
    "typeRoots": ["./node_modules/@types", "./src/types"]
  },
  "include": ["src", "src/types"]
}
5

Cannot Find Module in Monorepo

Turborepo · pnpm workspaces · package references
4 min
tsconfig.json — project references for monorepos
// Root tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@myapp/utils": ["./packages/utils/src"],
      "@myapp/ui": ["./packages/ui/src"]
    }
  },
  "references": [
    { "path": "./packages/utils" },
    { "path": "./packages/ui" }
  ]
}

// packages/utils/package.json — ensure main/exports/types are set
{
  "name": "@myapp/utils",
  "main": "./src/index.ts",
  "types": "./src/index.ts",
  "exports": {
    ".": "./src/index.ts"
  }
}

In pnpm workspaces, make sure each internal package is listed in the consumer's package.json dependencies as "@myapp/utils": "workspace:*".

6

VS Code Shows Error but Build Works

TypeScript version mismatch between editor and project
2 min

VS Code ships with its own TypeScript version. If your project's typescript version differs, you see editor errors that do not appear in the build.

VS Code — use workspace TypeScript
# 1. Open Command Palette: Cmd+Shift+P (Mac) / Ctrl+Shift+P (Windows)
# 2. Type: TypeScript: Select TypeScript Version
# 3. Choose: Use Workspace Version
# This forces VS Code to use your project's node_modules/typescript

# Alternatively, set in .vscode/settings.json:
{
  "typescript.tsdk": "node_modules/typescript/lib"
}

Prevention Checklist

Frequently Asked Questions

What is TypeScript error TS2307 cannot find module?+
TS2307 means TypeScript cannot locate the module you are importing. Either the package is not installed, it lacks type declarations, your tsconfig paths are misconfigured, or moduleResolution is set incorrectly for your bundler/runtime.
TypeScript cannot find module but it is installed — why?+
The package exists in node_modules but lacks TypeScript type declarations. Check if a corresponding @types/package-name exists on npm and install it. If not, create a declaration file: declare module 'package-name'; in a .d.ts file.
How do I fix path alias 'cannot find module @/components'?+
Set paths in tsconfig.json: { "@/*": ["./src/*"] }. Also configure the same alias in your bundler — Vite needs alias in vite.config.ts, webpack needs resolve.alias, and Next.js reads tsconfig.json paths natively.
What is the correct moduleResolution for Next.js 2026?+
Use moduleResolution: 'bundler' for Next.js 14+ and Vite projects, or 'node16'/'nodenext' for pure Node.js projects with ESM. The legacy 'node' setting does not support subpath exports and causes module resolution failures with modern packages.
Cannot find module only in VS Code but builds fine — why?+
VS Code uses its own TypeScript server which may be using a different TypeScript version than your project. Press Cmd+Shift+P → 'TypeScript: Select TypeScript Version' → 'Use Workspace Version'. This forces VS Code to use your project's node_modules typescript.
How do I create a type declaration for a package that has no @types?+
Create a file src/types/packages.d.ts and add: declare module 'package-name'; This tells TypeScript to treat the module as any type. For better types, declare module 'package-name' { export function myFn(): void; } with the actual exports.

Need Expert Help?

We Configure TypeScript Projects From Scratch

Softplix sets up production-ready TypeScript configurations for Next.js, Vite, and Node.js projects. Strict mode, path aliases, and monorepo support included.

Talk to an Engineer