How to fix "Module Not Found" error in Next.js?

When running npm run dev or next build, I get the following error:

ModuleNotFoundError: Module not found: Error: Can't resolve 'some-package' in '/path/to/project'

or

Error: Cannot find module 'some-package'
Require stack:
- /path/to/project/.next/server/pages/index.js
- /path/to/project/node_modules/next/dist/next-server/server/next-server.js
- /path/to/project/node_modules/next/dist/server/next.js
- /path/to/project/server.js

This happens even though I have installed the package and correctly imported the module.

Solution

It's not really clear from your description, this could be caused by many factors. Here are a few solutions you can try:

1. Check the Module Path

Ensure the import statement is correct and matches the actual file name (case-sensitive in Linux/macOS).

import myComponent from './components/MyComponent'; // ✅ Correct

If the file is named mycomponent.js, but you import MyComponent.js, it will fail on Linux/macOS.

2. Reinstall Dependencies

Sometimes, corrupted node_modules or package-lock.json can cause this issue. Run:

rm -rf node_modules package-lock.json && npm install

Or for Yarn:

rm -rf node_modules yarn.lock && yarn install

3. Missing Dependency

If the error mentions a missing package, install it explicitly:

npm install some-package

4. Check for Typos in Import Statements

If you misspell the module name in your import, you will get this error. Double-check your imports.

5. Clear Next.js Cache

Sometimes, Next.js caches build artifacts that cause errors. Try:

next build --no-cache

6. Check tsconfig.json or jsconfig.json (if using TypeScript or absolute imports)

If you're using absolute imports, ensure your tsconfig.json or jsconfig.json contains:

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

Then restart the dev server.

7. Use require.resolve to Debug

Run this in your project root to check if the module is installed:

node -e "console.log(require.resolve('some-package'))"

If it fails, the package is not installed correctly.

If none of these solutions work, try deleting the .next/ directory and restarting the project.

Alternative #1

I've been dealing with Next.js module resolution issues, and one thing that often gets overlooked is the Node.js module resolution algorithm. Sometimes the issue isn't with your code but with how Node.js is resolving the modules.

Try this debugging approach:

# Check if the module exists in node_modules
ls node_modules/some-package

# Check the actual path Node.js is trying to resolve
node -e "console.log(require.resolve('some-package'))"

# If that fails, check what's in your package.json
cat package.json | grep some-package

Also, check if you have any peer dependency conflicts. I've seen this happen with React versions:

npm ls react
npm ls react-dom

If you see multiple versions, that's your problem. You can fix it with:

npm dedupe

This consolidates duplicate packages and often resolves module resolution issues.

Alternative #2

Another common cause I've encountered is ESM vs CommonJS module conflicts. If you're mixing import/export with require/module.exports, you can get weird resolution issues.

Check your package.json for the "type" field:

{
  "type": "module"  // This forces ESM
}

If you have this, make sure all your imports use ESM syntax:

// ✅ ESM syntax
import { something } from 'package';
export default MyComponent;

// ❌ CommonJS syntax (won't work with "type": "module")
const { something } = require('package');
module.exports = MyComponent;

Or if you need CommonJS, remove the "type": "module" from package.json.

Also, check if your package has both .js and .mjs files - sometimes the wrong one gets imported.

Alternative #3

If you're using monorepos or workspaces, module resolution can get really tricky. I've spent hours debugging this in our company's setup.

Check if you have a lerna.json or workspace configuration:

// package.json
{
  "workspaces": [
    "packages/*"
  ]
}

In monorepos, you might need to:

  1. Hoist dependencies to the root:
npm install --workspace-root
  1. Check workspace symlinks:
ls -la node_modules/some-package
# Should show a symlink, not a regular directory
  1. Use workspace protocol in package.json:
{
  "dependencies": {
    "my-package": "workspace:*"
  }
}
  1. Clear all workspace caches:
rm -rf node_modules .next packages/*/node_modules
npm install

This is especially common with tools like Nx, Lerna, or Yarn workspaces.

Last modified: March 31, 2025
Stay in the loop
Subscribe to our newsletter to get the latest articles delivered to your inbox