Skip to content

Resolving Material-UI and React 18 Peer Dependency Conflict

When installing dependencies with npm, you may encounter a peer dependency conflict error when using older versions of Material-UI with React 18. This article explains the root cause and provides several solutions to resolve this issue.

Understanding the Problem

The error occurs because @material-ui/core@4.12.4 has a peer dependency specification that only supports React 16.8.0 to 17.0.0, while your project is using React 18.1.0:

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

npm's dependency resolution system is preventing the installation because the React version in your project doesn't satisfy Material-UI's requirements.

Solutions

The most sustainable solution is to upgrade to Material-UI v5 (now called MUI), which officially supports React 18:

bash
# Uninstall old Material-UI packages
npm uninstall @material-ui/core @material-ui/icons @material-ui/lab

# Install MUI v5 with required dependencies
npm install @mui/material @emotion/react @emotion/styled @popperjs/core

After installation, you'll need to update your imports throughout your codebase:

javascript
// Before (v4)
import { Button } from '@material-ui/core';

// After (v5)
import { Button } from '@mui/material';

TIP

MUI v5 uses Emotion as its default styling engine instead of JSS, which may require additional changes to your styling approach.

2. Use Legacy Peer Deps Flag (Temporary Solution)

If you need a quick workaround, you can use npm's legacy peer dependencies flag:

bash
npm install --legacy-peer-deps

This flag tells npm to ignore peer dependency conflicts and proceed with installation. Alternatively, you can configure npm to always use this behavior:

bash
npm config set legacy-peer-deps true
npm install

WARNING

Using --legacy-peer-deps may result in runtime issues if there are actual compatibility problems between the packages.

3. Switch to Yarn Package Manager

Yarn has a different approach to dependency resolution that may handle this conflict better:

bash
# Install Yarn globally if needed
npm install -g yarn

# Remove node_modules and install with Yarn
rm -rf node_modules
yarn install

While not ideal, you could downgrade React to a version compatible with Material-UI v4:

json
{
  "dependencies": {
    "react": "^17.0.0",
    "react-dom": "^17.0.0"
  }
}

DANGER

Downgrading React may cause compatibility issues with other packages in your project that require React 18.

Understanding Peer Dependencies

Peer dependencies are packages that your library needs to work properly but doesn't want to include directly. They ensure that consumers of your library have the required compatible versions installed.

In your case:

  • Your project has React 18.1.0
  • Material-UI v4 requires React 16.8.0 || 17.0.0
  • npm detects this version mismatch and prevents installation

The legacy-peer-deps flag reverts to npm's older behavior where peer dependencies were less strictly enforced.

Best Practices

  1. Keep dependencies updated: Regularly update your packages to avoid compatibility issues
  2. Check compatibility: Before updating major versions of key dependencies, verify that all your packages support the new version
  3. Use version ranges wisely: Specify appropriate version ranges in your package.json to allow for updates while maintaining compatibility

Troubleshooting Deployment Issues

If you encounter this error during deployment (e.g., on Heroku):

  1. Delete and recreate your deployment environment
  2. Ensure your build process includes the --legacy-peer-deps flag if needed
  3. Verify that your package.json doesn't contain syntax errors or incompatible comments

Conclusion

The most recommended solution is to upgrade to Material-UI v5, which officially supports React 18 and provides better performance and features. If you need a temporary solution, using --legacy-peer-deps can unblock your development, but be aware of potential runtime compatibility issues.

For long-term projects, maintaining updated dependencies and following semantic versioning best practices will help prevent similar issues in the future.