Resolving ‘Module not found: Error: Can’t resolve react-dom’
This error occurs when your project can’t locate the react-dom
package, which is a core dependency for React web applications. Here’s how to properly fix this issue.
Common Causes
- Missing Dependency:
react-dom
isn’t installed - Version Mismatch: Incompatible React and React DOM versions
- Installation Corruption: Broken node_modules
- Configuration Issues: Incorrect webpack/babel setup
- Path Problems: Module resolution failing
Step-by-Step Solutions
1. Install react-dom (Most Common Fix)
# For npm users
npm install react-dom
# For yarn users
yarn add react-dom
# If you need a specific version
npm install react-dom@18.2.0
2. Verify Version Compatibility
Ensure react
and react-dom
versions match in your package.json
:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
3. Clean and Reinstall Dependencies
# Remove node_modules and lock files
rm -rf node_modules package-lock.json yarn.lock
# Fresh install
npm install
4. Check Import Statements
Verify correct imports in your files:
// Client-side rendering (usual case)
import ReactDOM from 'react-dom/client';
// For React 17 and below
import ReactDOM from 'react-dom';
5. Webpack Configuration (If Using Custom Setup)
Ensure your webpack config includes:
// webpack.config.js
module.exports = {
resolve: {
extensions: ['.js', '.jsx']
}
};
Advanced Troubleshooting
1. Peer Dependency Warnings
# Check for peer dependency issues
npm ls react-dom
2. Monorepo Considerations
If using a monorepo (like Lerna), ensure:
# Install in the correct workspace
lerna add react-dom --scope=your-package
3. TypeScript Projects
Add types if missing:
npm install --save-dev @types/react-dom
Prevention Tips
- Use Create React App for standard setups
- Keep dependencies updated regularly
- Document installation steps in your README
- Set up CI checks to catch installation issues
- Use package managers consistently (don’t mix npm/yarn)
When Using React 18+
Note the new client API:
// src/index.js
import { createRoot } from 'react-dom/client';
import App from './App';
const root = createRoot(document.getElementById('root'));
root.render(<App />);
Remember that react-dom
is:
- Required for web applications
- Separate from
react-native
for mobile - Version-locked with React core
- Essential for DOM rendering
If you’re still experiencing issues after trying these solutions:
- Check your Node.js version (
node -v
) - Verify network connectivity during installation
- Consult your framework’s documentation (Next.js, Gatsby, etc.)
- Search for similar issues in your project’s GitHub repository