TypeScript Cannot Find Module— Fixed [Complete Guide 2026]
Also covers: TS2307 · cannot find type declarations · path aliases · moduleResolution
⚡ The Errors You Are Seeing
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:
- Package installed but missing type declarations (no @types/package)
- Path aliases like @/ configured in tsconfig but not in the bundler, or vice versa
- moduleResolution in tsconfig.json is wrong for your bundler or runtime
- Importing a file without an extension that TypeScript cannot find
Missing @types Package
Cannot find type declarations for 'lodash', 'express', etc.Many npm packages ship JavaScript only, without TypeScript types. The DefinitelyTyped project provides @types/* packages that add types for these.
# 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){
"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.
Path Aliases Not Configured
@/ aliases · import from '@/components/Button'Path aliases require configuration in two places: tsconfig.json (for TypeScript to understand) AND your bundler config (for the runtime to resolve them).
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
"@components/*": ["./src/components/*"],
"@lib/*": ["./src/lib/*"],
"@types/*": ["./src/types/*"]
}
}
}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.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;
},
};moduleResolution Set Incorrectly
Cannot find module for packages with exports fieldModern packages use the exports field in package.json for subpath exports. The legacy node moduleResolution does not support this — upgrade to bundler or node16.
// 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.
Missing Declaration File (.d.ts)
Package with no @types — write your own declarationsWhen a package has no @types equivalent, create a local declaration file to silence the error.
// 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;
}{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./src/types"]
},
"include": ["src", "src/types"]
}Cannot Find Module in Monorepo
Turborepo · pnpm workspaces · package references// 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:*".
VS Code Shows Error but Build Works
TypeScript version mismatch between editor and projectVS 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.
# 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
- Set moduleResolution: 'bundler' for Vite/Next.js projects from the start
- Configure path aliases in both tsconfig.json AND your bundler config simultaneously
- Install @types/node when using Node.js built-in modules like path, fs, os
- Use 'TypeScript: Select TypeScript Version → Workspace Version' in VS Code
- In monorepos, always set main, types, and exports in each package's package.json
- Create a src/types/ directory for vendor declarations — keep them organized
Frequently Asked Questions
What is TypeScript error TS2307 cannot find module?+−
TypeScript cannot find module but it is installed — why?+−
How do I fix path alias 'cannot find module @/components'?+−
What is the correct moduleResolution for Next.js 2026?+−
Cannot find module only in VS Code but builds fine — why?+−
How do I create a type declaration for a package that has no @types?+−
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