Introduction

Webpack cannot resolve when import path incorrect or module not installed. This guide provides step-by-step diagnosis and resolution.

Symptoms

Typical error output:

bash
ERROR in ./src/index.js
Module not found: Error: Cannot resolve ./components/App in /project/src
Module build failed: File not found.

Common Causes

  1. 1.Component configuration or import path incorrect
  2. 2.Framework hooks or lifecycle methods misused
  3. 3.CSS configuration or class syntax error
  4. 4.Build tool configuration or dependency missing

Step-by-Step Fix

Step 1: Check Current State

bash
# Check build status
npm run build 2>&1 | head -20
# View browser console
# Open Developer Tools > Console
# Check node modules
ls node_modules/

Step 2: Identify Root Cause

bash
# Check package.json
cat package.json
# Verify dependencies
npm list --depth=0
# Check build config
cat webpack.config.js

Step 3: Apply Primary Fix

```bash # Primary fix: Check and rebuild # Clear node modules rm -rf node_modules package-lock.json npm install

# Rebuild npm run build

# Start dev server npm run dev ```

Step 4: Apply Alternative Fix

bash
# Alternative: Debug in browser
# Open Chrome DevTools > Components
# Check React props and state
# Debug Vue with vue-devtools
# Verify template syntax

Step 5: Verify the Fix

bash
npm run build
# Should complete without errors
npm run dev
# Open localhost:3000, check console

Common Pitfalls

  • Not clearing node_modules when dependency issues
  • Misusing framework hooks outside components
  • Incorrect CSS class syntax or config
  • Not checking browser console for errors

Best Practices

  • Use TypeScript for type safety
  • Keep dependencies updated
  • Configure linting and formatting
  • Test components in isolation
  • Component Not Rendering
  • Build Failed
  • Dependency Error
  • CSS Not Applying