Fixing Cannot find module 'next/dist/shared/lib/router/utils/routing-items.js': Root Cause and Resolution
This exception occurs when the Next.js runtime attempts to resolve internal router utilities that do not exist in the installed version of the `next` package. It is typically triggered by a version...
Fixing Cannot find module 'next/dist/shared/lib/router/utils/routing-items.js': Root Cause and Resolution
Problem Overview
This exception occurs when the Next.js runtime attempts to resolve internal router utilities that do not exist in the installed version of the next package. It is typically triggered by a version mismatch between the next dependency and the @next/react-dev-overlay or @next/font packages, or by a corrupted node_modules directory following a partial installation.
Root Cause
- Trigger: The
nextpackage version inpackage.jsondoes not match the actual files present innode_modules/next/dist/shared/lib/router/utils/. Specifically,routing-items.jswas introduced or renamed in specific Next.js versions (e.g., v13.4+ or v14.x), but the installed binary tree is from an older or incompatible version. - Runtime Behavior: The Node.js module resolver (
requireorimport) fails to locate the file path within thenextpackage's distribution folder. This causes an immediateMODULE_NOT_FOUNDerror during the server startup or client-side hydration phase, halting the application process.
Step-by-Step Fix
Step 1: Diagnose the State
Run the following command in your terminal to inspect the environment:
npm ls next
Verify that the installed version matches the version specified in package.json. If the output shows UNMET DEPENDENCY or a version mismatch, proceed to Step 2.
Step 2: Apply the Fix
Clear the local cache and reinstall dependencies to ensure a consistent module tree. Execute the following commands in sequence:
rm -rf node_modules rm -rf .next npm cache clean --force npm install
If using Yarn or pnpm, replace npm install with yarn install or pnpm install respectively.
Step 3: Verify the Resolution
Confirm the fix worked by running:
npm run dev
The development server should start without throwing the MODULE_NOT_FOUND error. Access the application in the browser to ensure the router initializes correctly.
Prevention
- Pin the
nextversion inpackage.jsonto avoid unintended major version upgrades duringnpm update. - Use a lockfile (
package-lock.json,yarn.lock, orpnpm-lock.yaml) and commit it to version control to ensure reproducible builds. - Avoid manually editing files within
node_modules/next/dist/. All internal changes must be managed through thenextpackage version. - When upgrading Next.js, review the migration guide for breaking changes in internal module paths and update any direct imports of
next/distmodules accordingly.