This error occurs when your project has incompatible versions of React packages (react, react-dom, or other React libraries) installed. It commonly manifests as peer dependency conflicts, invalid hook calls, or build failures when different packages require different React versions.
A React version mismatch error indicates that your project has multiple conflicting versions of React-related packages installed simultaneously. React applications require that `react` and `react-dom` (or other renderers like `react-native`) must use the exact same version. This strict requirement exists because React's internal architecture relies on shared state and hooks that break when different versions are mixed. The error typically surfaces during npm/yarn installations when peer dependencies cannot be satisfied, or at runtime when hooks fail with "Invalid hook call" warnings. The mismatch can occur when upgrading React, adding new libraries that depend on older React versions, or when npm/yarn resolves dependencies incorrectly, creating duplicate React installations in node_modules. Understanding the npm dependency tree is crucial here: even if your package.json specifies matching versions, nested dependencies from third-party packages can pull in conflicting React versions. Modern package managers try to deduplicate packages, but strict version constraints or outdated libraries can prevent proper resolution.
First, identify which React versions are currently installed in your project:
npm ls react
npm ls react-domThis shows your dependency tree and highlights any duplicate installations. Look for multiple versions listed at different dependency depths. You can also check top-level dependencies:
npm ls --depth=0 | grep reactIf you see multiple versions, note which packages are causing the duplication.
Open your package.json and ensure react and react-dom specify the same version without the caret (^) or tilde (~) prefix:
{
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
}
}Remove the ^ character to prevent automatic minor version updates that could cause mismatches. If you're using React Native, include it as well:
{
"dependencies": {
"react": "18.2.0",
"react-native": "0.72.0"
}
}Delete your node_modules and lock file, then reinstall from scratch:
# Remove existing installations
rm -rf node_modules
rm package-lock.json # or yarn.lock / pnpm-lock.yaml
# Clear npm cache
npm cache clean --force
# Reinstall
npm installFor yarn users:
rm -rf node_modules
rm yarn.lock
yarn cache clean
yarn installThis ensures a fresh dependency resolution without conflicts from cached data.
If multiple React versions persist after reinstalling, use npm's deduplication tool:
npm dedupeThis attempts to reduce duplication by moving dependencies to the top level where possible. After running, verify the result:
npm ls reactYou should see a single React version throughout the tree. If duplicates remain, you may have packages with incompatible peer dependency requirements.
Check which packages have outdated React requirements:
npm outdatedUpdate packages that have newer versions supporting your React version:
npm update <package-name>For packages without updates, check their GitHub issues or documentation for React 19 compatibility. You may need to find alternative libraries or wait for updates. Use npx npm-check-updates to see major version updates:
npx npm-check-updates
npx npm-check-updates -u # Update package.json
npm installIf some dependencies require older React versions but work fine with your current version, force resolution using package manager overrides.
For npm (v8.3+), add to package.json:
{
"overrides": {
"react": "18.2.0",
"react-dom": "18.2.0"
}
}For yarn, use resolutions:
{
"resolutions": {
"react": "18.2.0",
"react-dom": "18.2.0"
}
}For pnpm:
{
"pnpm": {
"overrides": {
"react": "18.2.0",
"react-dom": "18.2.0"
}
}
}Then reinstall dependencies. Use this carefully as it may cause runtime issues with packages expecting different React APIs.
If you encounter ERESOLVE errors during installation, you have temporary workarounds:
npm install --legacy-peer-depsThis ignores peer dependency conflicts but may lead to runtime errors. A better approach:
npm install --forceThis forces installation despite conflicts. However, the proper fix is updating or replacing incompatible packages rather than suppressing warnings. Only use these flags as a last resort for testing whether the mismatch is actually causing problems.
React Native Projects: Use Expo's built-in tools if applicable. Run npx expo-doctor to detect version issues, and npx expo install --fix to automatically resolve React Native version conflicts according to your SDK version.
Monorepo Considerations: In monorepos (Lerna, Nx, Turborepo), ensure hoisting is configured properly so React installs only at the root. Set hoist: true in your workspace configuration and use resolutions or overrides to enforce a single React version across all packages.
Development vs Production: Sometimes this error only appears in development when using tools like npm link or yarn link for local package development. This creates symlinks that result in multiple React copies. Use Yalc (npm i -g yalc) as an alternative to linking for local development.
Bundle Analysis: If the error occurs in production builds, analyze your bundle to check if multiple React versions are being bundled. Use webpack-bundle-analyzer or similar tools. Configure webpack aliases to ensure all React imports resolve to a single location:
// webpack.config.js
module.exports = {
resolve: {
alias: {
react: path.resolve('./node_modules/react'),
'react-dom': path.resolve('./node_modules/react-dom'),
},
},
};Prevention: Pin exact versions in package.json for React packages, use package-lock.json in version control, run npm ci in CI/CD instead of npm install to ensure reproducible builds, and regularly update dependencies to stay current with library compatibility updates.
React Hook useCallback has a missing dependency: 'variable'. Either include it or remove the dependency array react-hooks/exhaustive-deps
React Hook useCallback has a missing dependency
Cannot use private fields in class components without TS support
Cannot use private fields in class components without TS support
Cannot destructure property 'xxx' of 'undefined'
Cannot destructure property of undefined when accessing props
useNavigate() may be used only in the context of a <Router> component.
useNavigate() may be used only in the context of a Router component
Cannot find module or its corresponding type declarations
How to fix "Cannot find module or type declarations" in Vite