This TypeScript error occurs when the compiler cannot locate the Next.js module or its type definitions. It typically happens due to missing installations, incorrect TypeScript configuration, or conflicts between Next.js versions and moduleResolution settings.
The 'Cannot find module 'next'' error indicates that TypeScript's module resolution system failed to locate the Next.js package or its type definitions. This error commonly appears when importing from 'next', 'next/router', 'next/head', 'next/link', or other Next.js modules. Since Next.js v12, the framework includes built-in TypeScript types, eliminating the need for a separate @types/next package. However, specific TypeScript configuration settings—particularly moduleResolution strategies like 'nodenext' or 'node16'—can cause compatibility issues with Next.js's module structure. The error prevents TypeScript from understanding Next.js APIs and may block development or build processes.
Check that 'next', 'react', and 'react-dom' are installed as dependencies:
npm list next react react-domIf any are missing, install them:
npm install next react react-dom
# or
yarn add next react react-dom
# or
pnpm add next react react-domVerify the versions are compatible. For Next.js 14+, use React 18+.
Next.js v12 and later include built-in TypeScript types. Having @types/next installed can cause conflicts:
npm uninstall @types/next
# or
yarn remove @types/next
# or
pnpm remove @types/nextCheck package.json to ensure @types/next is removed from devDependencies:
{
"devDependencies": {
// ❌ Remove this line if present
"@types/next": "..."
}
}Next.js works best with 'bundler' or 'node' module resolution. Update your tsconfig.json:
{
"compilerOptions": {
"moduleResolution": "bundler", // Recommended for Next.js 13+
"module": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
}
}Important: Avoid using 'node16' or 'nodenext' moduleResolution with Next.js, as these cause compatibility issues with Next.js's package structure.
Corrupted dependencies can cause module resolution failures. Delete and reinstall:
# Remove existing dependencies
rm -rf node_modules
rm package-lock.json # or yarn.lock / pnpm-lock.yaml
# Reinstall
npm install
# or
yarn install
# or
pnpm installAfter reinstalling, verify Next.js is present:
ls node_modules/next/package.jsonYour IDE may cache outdated module information. Restart the TypeScript language server:
VS Code:
1. Press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows/Linux)
2. Type 'TypeScript: Restart TS Server'
3. Press Enter
WebStorm:
1. Right-click the tsconfig.json file
2. Select 'Restart TypeScript Service'
Alternatively, restart your IDE completely.
Ensure your tsconfig.json properly extends Next.js's base configuration:
{
"extends": "./node_modules/next/tsconfig.json",
"compilerOptions": {
// Your custom options here
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}The .next/types directory is created during development and contains generated types for Next.js.
Module Resolution Compatibility: Next.js uses a complex module structure with conditional exports, which is incompatible with TypeScript's 'node16' and 'nodenext' moduleResolution strategies. This is a known limitation tracked in GitHub issue #46078. For modern Next.js projects (v13+), use 'bundler' moduleResolution, which was designed for bundler-based tools like Next.js, Vite, and webpack. Type Generation: Next.js generates types at runtime in the .next/types directory based on your app structure (App Router vs Pages Router). These types are essential for features like typed route parameters and dynamic imports. Ensure your dev server has run at least once to generate these types. Monorepo Considerations: In monorepo setups with shared packages, ensure each workspace has its own tsconfig.json and that cross-package imports use proper TypeScript project references. Path aliases configured in tsconfig.json should also be mirrored in next.config.js for runtime resolution. Version Compatibility: Next.js 14 requires TypeScript 5.0+, while Next.js 13 works with TypeScript 4.5+. Check the official Next.js TypeScript documentation for version requirements.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript