Published on

Fix Could Not Resolve Dependency Error in React

Authors
  • Name
    Ripal & Zalak
    Twitter

Fix: Could Not Resolve Dependency Error in React with Material-UI

Introduction

If you're encountering an error like:

npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.0 || ^17.0.0" from @material-ui/core@4.12.4

This typically happens when using React 18 with an older version of Material-UI (MUI v4) that does not support React 18. In this guide, we'll explore the causes and solutions for fixing this dependency issue.


Why This Happens?

  • Material-UI v4 only supports React 16.x or 17.x.
  • Your project is using React 18, which causes a version mismatch.
  • NPM enforces stricter dependency rules, leading to a failure in package installation.

Solution 1: Use --legacy-peer-deps or --force

If you don’t want to downgrade React, force NPM to ignore peer dependencies.

npm install --legacy-peer-deps

Or:

npm install --force

✅ This allows installation but may cause compatibility issues.


Instead of forcing dependencies, upgrade to MUI v5, which supports React 18.

Steps to Upgrade

  1. Uninstall Old Material-UI Packages:

    npm uninstall @material-ui/core @material-ui/icons @material-ui/lab
    
  2. Install MUI v5 and Dependencies:

    npm install @mui/material @emotion/react @emotion/styled @mui/icons-material
    
  3. If Using Popper.js: Install Popper.js for better tooltips:

    npm install @popperjs/core
    
  4. Update Imports in Your Code: Change all @material-ui/core imports to @mui/material.

    Before:

    import { Button } from '@material-ui/core'
    

    After:

    import { Button } from '@mui/material'
    

✅ Now your project should work fine with React 18.


Solution 3: Downgrade React to v17

If upgrading MUI isn’t an option, you can downgrade React to v17.

Steps to Downgrade React

  1. Uninstall React 18:
    npm uninstall react react-dom
    
  2. Install React 17:
    npm install react@17 react-dom@17
    
  3. Ensure Compatibility: Check package.json and update peerDependencies:
    "peerDependencies": {
       "react": "^16.8.0 || ^17.0.0",
       "react-dom": "^16.8.0 || ^17.0.0"
    }
    

✅ This ensures your project works with Material-UI v4.


Solution 4: Use Yarn Instead of NPM

Yarn can resolve dependencies more flexibly than NPM.

npm uninstall -g yarn  # Remove old version
npm install -g yarn    # Install latest Yarn

Then, install dependencies with:

yarn install

✅ This may help resolve dependency conflicts automatically.


Final Debugging Checklist

✔ Check if your project needs React 18 or if React 17 is sufficient. ✔ Verify the package.json dependencies and peer dependencies. ✔ Use npm outdated to see if dependencies need updates. ✔ Try running npm cache clean --force before reinstalling. ✔ Ensure you are using the latest Node.js and NPM versions.


Conclusion

If you face dependency resolution errors while using Material-UI v4 with React 18, the best solution is upgrading to MUI v5. If that’s not an option, you can downgrade React or use legacy-peer-deps as a workaround.

Following these steps should help you resolve the error and get your project running smoothly!


FAQs

1. Why does NPM throw a dependency conflict error?

NPM enforces strict dependency rules. If a package requires a different version than what’s installed, it fails to resolve dependencies.

2. Is using --legacy-peer-deps safe?

It allows package installation but may lead to compatibility issues. It’s better to upgrade dependencies when possible.

3. Can I use Material-UI v4 with React 18?

No, MUI v4 does not officially support React 18. Upgrading to MUI v5 is recommended.

4. What if I can’t upgrade Material-UI?

Consider downgrading React to v17, which is compatible with Material-UI v4.