How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Next.js · TypeScript/React

Fix ModuleNotFoundError: Module not found: Can't resolve '@/components/Header' in Next.js

This error occurs when Next.js cannot resolve a module path, usually because the path alias is misconfigured in tsconfig.json or the file does not exist at the expected location. Fix it by verifying the paths mapping in tsconfig.json matches your directory structure and the target file exists.

Reading the Stack Trace

ModuleNotFoundError: Module not found: Error: Can't resolve '@/components/Header' in '/app/src/pages' at /app/node_modules/webpack/lib/Compilation.js:2016:28 at /app/node_modules/webpack/lib/NormalModuleFactory.js:798:13 at eval (eval at create (/app/node_modules/tapable/lib/HookCodeFactory.js:33:10)) at /app/node_modules/enhanced-resolve/lib/Resolver.js:312:5 at /app/node_modules/enhanced-resolve/lib/Resolver.js:389:13 at /app/node_modules/enhanced-resolve/lib/forEachBail.js:16:9 at /app/node_modules/enhanced-resolve/lib/AliasPlugin.js:46:12 at /app/node_modules/enhanced-resolve/lib/Resolver.js:389:13 at processResult (/app/node_modules/webpack/lib/NormalModuleFactory.js:68:17) at /app/node_modules/webpack/lib/NormalModuleFactory.js:342:5

Here's what each line means:

Common Causes

1. Missing paths alias in tsconfig.json

The tsconfig.json does not include a paths entry for the @ alias, so Next.js and webpack cannot resolve the import.

// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2017",
    "module": "esnext"
    // No paths or baseUrl configured
  }
}

2. File does not exist at the expected path

The import path is correct but the file was renamed or moved without updating the import statements.

// src/pages/index.tsx
import Header from '@/components/Header'; // File was renamed to NavHeader.tsx

3. Incorrect file extension

The file exists but with a different extension than what webpack is configured to resolve.

// The file is Header.jsx but tsconfig resolves only .ts and .tsx
import Header from '@/components/Header';

The Fix

Add baseUrl and paths to tsconfig.json so that the @ alias resolves to the src directory. This allows webpack and TypeScript to find modules imported with the @/ prefix.

Before (broken)
// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2017",
    "module": "esnext",
    "jsx": "preserve"
  },
  "include": ["src"]
}
After (fixed)
// tsconfig.json
{
  "compilerOptions": {
    "target": "ES2017",
    "module": "esnext",
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src"]
}

Testing the Fix

import { render, screen } from '@testing-library/react';
import Header from '@/components/Header';

describe('Header', () => {
  it('resolves the @/ alias and renders the header', () => {
    render(<Header />);
    expect(screen.getByRole('banner')).toBeInTheDocument();
  });

  it('renders navigation links', () => {
    render(<Header />);
    expect(screen.getByRole('navigation')).toBeInTheDocument();
  });
});

Run your tests:

npm test

Pushing Through CI/CD

git checkout -b fix/module-not-found-path-alias,git add tsconfig.json,git commit -m "fix: add baseUrl and paths alias to tsconfig.json",git push origin fix/module-not-found-path-alias

Your CI config should look something like this:

name: CI
on:
  pull_request:
    branches: [main]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm'
      - run: npm ci
      - run: npm test -- --coverage
      - run: npm run build

The Full Manual Process: 18 Steps

Here's every step you just went through to fix this one bug:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. Monitor production to confirm the error is resolved

Total time: 30-60 minutes. For one bug.

Or Let bugstack Fix It in Under 2 minutes

Every step above? bugstack does it automatically.

Step 1: Install the SDK

npm install bugstack-sdk

Step 2: Initialize

import { initBugStack } from 'bugstack-sdk'

initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })

Step 3: There is no step 3.

bugstack handles everything from here:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. Deploys to production (or opens a PR for review)

Time from error to fix deployed: Under 2 minutes.

Human involvement: zero.

Try bugstack Free →

No credit card. 5-minute setup. Cancel anytime.

Deploying the Fix (Manual Path)

  1. Verify tsconfig.json has the correct paths alias configuration.
  2. Run the Next.js build locally to confirm all imports resolve.
  3. Open a pull request with the tsconfig.json changes.
  4. Wait for CI checks to pass on the PR.
  5. Merge to main and verify the build succeeds in staging.

Frequently Asked Questions

BugStack validates that all module imports resolve correctly, runs your test suite, and confirms the Next.js build completes without errors before marking the fix safe.

Every fix is delivered as a pull request with full CI validation. Your team reviews and approves before anything reaches production.

Yes, if the paths alias is missing, both next dev and next build will fail with the same ModuleNotFoundError since they share the same tsconfig.json.

No. Adding baseUrl and paths only enables alias resolution. Existing relative imports continue to work exactly as before.