How to fix Error: Peer dependency 'react' required but not installed in Node.js
This error occurs when a package requires a peer dependency that is not installed in your project. Starting with npm 7, peer dependencies must be installed manually, and npm will fail the installation if they are missing.
Quick fix
Fixes Error: Peer dependency 'react' required but not installed
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^18.0.0" from some-package@1.0.0
npm ERR! node_modules/some-package- Identify the missing peer dependency
- Install the missing peer dependency
- Use install-peerdeps for automatic installation
- Check for version conflicts
npm ERR! Could not resolve dependency:npm ERR! peer react@"^18.0.0" from some-package@1.0.0npm ERR! node_modules/some-packageError: Peer dependency 'react' required but not installed
What this error means
This error indicates that a package you're trying to install or use has declared a peer dependency that is not present in your project's node_modules. Peer dependencies are packages that a library expects to be provided by the consuming application rather than bundling them directly. This is common with frameworks like React, where multiple packages need to share the same instance of the framework to work correctly. Starting with npm 7, peer dependencies are enforced strictly. If a peer dependency is missing or has an incompatible version, npm will fail the installation and show this error. This is different from npm 3-6, which only showed warnings.
Typical symptoms
- npm install command fails with peer dependency error
- Package installation stops with "ERESOLVE unable to resolve dependency tree"
- Error message lists specific peer dependency and required version range
- Installation works with --legacy-peer-deps or --force flags
- CI/CD pipeline fails during dependency installation
- Fresh clone of repository fails to install dependencies
Common causes
- The peer dependency package is not installed in your project
- An incompatible version of the peer dependency is installed
- package-lock.json contains outdated or conflicting dependency resolutions
- Upgrading npm from version 6 to 7+ with existing projects
- Installing a package that requires a newer version of a peer dependency than your project uses
- Missing peer dependencies after deleting node_modules and reinstalling
How to fix "Error: Peer dependency 'react' required but not installed"
Read the error message carefully to identify which peer dependency is missing and what version is required:
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^18.0.0" from some-package@1.0.0
npm ERR! node_modules/some-packageThe error shows:
- Package name: react
- Required version: ^18.0.0
- Which package requires it: some-package@1.0.0
Install the peer dependency directly in your project:
npm install react@18Or if you need a specific version:
npm install react@18.2.0For development-only peer dependencies:
npm install --save-dev react@18After installation, run your original install command again.
The install-peerdeps utility automatically installs a package along with its peer dependencies:
npx install-peerdeps <package-name>For example:
npx install-peerdeps some-react-componentThis tool reads the package's peerDependencies and installs compatible versions automatically.
If you already have the peer dependency installed but with an incompatible version, check what's installed:
npm list reactCompare the installed version with the required version from the error. You may need to upgrade or downgrade:
# Upgrade to a compatible version
npm install react@latest
# Or install a specific compatible version
npm install react@18.2.0Check which packages depend on conflicting versions:
npm ls react --depth=0If you're still experiencing issues, try a clean installation:
# Remove node_modules and lock file
rm -rf node_modules package-lock.json
# Clear npm cache
npm cache clean --force
# Reinstall all dependencies
npm installThis ensures all dependencies are resolved fresh with current peer dependency requirements.
Advanced notes
Using --legacy-peer-deps
You can bypass peer dependency checks using the --legacy-peer-deps flag:
npm install --legacy-peer-depsThis makes npm behave like versions 4-6, ignoring peer dependency conflicts. However, this is not recommended for production as it can lead to runtime errors when packages expect specific peer dependency versions.
Using --force
The --force flag is more aggressive and will override all conflicts:
npm install --forceThis should be used as a last resort, as it can result in broken or incompatible packages.
Setting in .npmrc
To always use legacy peer deps behavior, add to your .npmrc file:
legacy-peer-deps=trueUnderstanding peer dependency ranges
Peer dependencies use semantic versioning ranges:
- ^18.0.0 - Compatible with 18.0.0 and above (but below 19.0.0)
- >=18.0.0 <19.0.0 - Explicitly between versions
- * - Any version (not recommended)
Checking peer dependencies with tools
Use check-peer-dependencies to audit your project:
npx check-peer-dependenciesThis recursively finds all peer dependencies and checks if compatible versions are installed.
Yarn and pnpm behavior
Different package managers handle peer dependencies differently:
- Yarn 1: Issues warnings like npm 3-6
- Yarn 2+: Strictly enforces peer dependencies
- pnpm: Always installs peer dependencies but may require --shamefully-hoist flag for compatibility
Why peer dependencies exist
Peer dependencies solve the "singleton" problem. For frameworks like React, having multiple copies loaded simultaneously causes errors. By declaring React as a peer dependency, component libraries ensure they use the same React instance as your application.
Frequently asked questions
- What does "Error: Peer dependency 'react' required but not installed" mean?
- This error indicates that a package you're trying to install or use has declared a peer dependency that is not present in your project's node_modules. Peer dependencies are packages that a library expects to be provided by the consuming application rather than bundling them directly. This is common with frameworks like React, where multiple packages need to share the same instance of the framework to work correctly.
- How do I fix "Error: Peer dependency 'react' required but not installed"?
- This error occurs when a package requires a peer dependency that is not installed in your project. Starting with npm 7, peer dependencies must be installed manually, and npm will fail the installation if they are missing.
- What causes "Error: Peer dependency 'react' required but not installed"?
- The peer dependency package is not installed in your project An incompatible version of the peer dependency is installed package-lock.json contains outdated or conflicting dependency resolutions
- Is the "Error: Peer dependency 'react' required but not installed" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Node.js Official Documentation
- ๐Node.js official peer dependencies documentation(Official Docs)
- ๐
- ๐