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
Here's what each line means:
- at /app/node_modules/webpack/lib/Compilation.js:2016:28: Webpack's compilation step failed because the module could not be resolved in the dependency graph.
- at /app/node_modules/enhanced-resolve/lib/AliasPlugin.js:46:12: The alias resolution plugin tried the configured path aliases but found no matching file.
- at /app/node_modules/enhanced-resolve/lib/Resolver.js:312:5: The resolver exhausted all configured paths and extensions without finding a valid module.
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.
// tsconfig.json
{
"compilerOptions": {
"target": "ES2017",
"module": "esnext",
"jsx": "preserve"
},
"include": ["src"]
}
// 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:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- 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:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- 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)
- Verify tsconfig.json has the correct paths alias configuration.
- Run the Next.js build locally to confirm all imports resolve.
- Open a pull request with the tsconfig.json changes.
- Wait for CI checks to pass on the PR.
- 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.