This error occurs when your build tool or development server cannot locate the React library in your project. It typically happens when React is not installed, node_modules is missing or corrupted, or your package.json dependencies are incomplete. Resolving this requires ensuring React is properly installed and your project's dependency tree is intact.
When webpack, Vite, or another bundler tries to compile your project, it needs to find all imported modules. The error "Module not found: Can't resolve 'react'" means the build tool cannot locate the React library that your code is trying to import. This happens during the module resolution phase, where the bundler searches for packages in the node_modules directory based on your import statements. If React is not installed, the node_modules folder is missing, or the package installation was interrupted or corrupted, the bundler will fail to find React and throw this error. This is a critical dependency error that prevents your project from building or running. The bundler expects to find React installed as a dependency in your project, and when it cannot locate the module, compilation halts immediately. For React projects, both the 'react' and 'react-dom' packages are essential dependencies. The error can also occur if you're running build commands from the wrong directory (not where package.json exists), or if your package manager's cache is corrupted.
Ensure your terminal is in the project root directory where package.json is located. List the contents of the current directory and confirm package.json exists:
ls -la
# You should see package.json in the output
pwd
# Verify this is your project's root directoryIf package.json is not in the current directory, navigate to the correct directory using cd before proceeding.
Open your package.json file and verify that "react" is listed under dependencies. It should look like this:
{
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}If React is missing from dependencies, add it manually or proceed to the next step to install it.
Install React and react-dom using npm or yarn. This will add them to package.json and create/update node_modules:
npm install react react-dom
# Or if using yarn:
yarn add react react-dom
# Or if using pnpm:
pnpm add react react-domAfter installation completes, try running your development server or build command again.
If installing React did not resolve the issue, perform a complete clean reinstall. This removes potentially corrupted files and rebuilds your dependency tree:
# Remove node_modules and lock file
rm -rf node_modules
rm -f package-lock.json
# Or for yarn: rm -f yarn.lock
# Or for pnpm: rm -f pnpm-lock.yaml
# Clear npm cache
npm cache clean --force
# Reinstall all dependencies
npm install
# Or for yarn:
yarn install
# Or for pnpm:
pnpm installThis forces a fresh installation of all packages from scratch.
Check that your import statements for React are formatted correctly. Common mistakes include typos or incorrect casing:
// CORRECT - lowercase "react"
import React from 'react';
import { useState } from 'react';
// WRONG - uppercase "React" in import path
import React from 'React'; // This will fail
// WRONG - incorrect module name
import React from 'reactjs'; // No such packageEnsure all files importing React use the correct lowercase 'react' module name.
If you are using a monorepo (with tools like Lerna, Nx, or Turborepo) or yarn/pnpm workspaces, ensure React is installed in the correct workspace package:
# For yarn workspaces, install in specific package:
yarn workspace my-app add react react-dom
# For pnpm workspaces:
pnpm add react react-dom --filter my-app
# Alternatively, install in root for all packages:
pnpm add react react-dom -wVerify your workspace configuration in package.json or the workspace root configuration file.
Older versions of npm have known dependency resolution bugs. Update npm to the latest version:
npm install -g npm@latest
# Verify the update
npm --versionAfter updating npm, delete node_modules and package-lock.json again, then run npm install.
After installing dependencies, completely restart your development server and IDE:
# Stop the dev server (Ctrl+C)
# Start it again
npm run dev
# Or: npm start
# Or: yarn devAlso close and reopen your code editor (VS Code, WebStorm, etc.) to ensure it reloads the updated node_modules.
In some edge cases, this error can occur with Create React App projects when there is a peer dependency conflict or when using incompatible versions of React and react-dom. Always ensure React and react-dom have matching version numbers. If you are using TypeScript, you may also need to install @types/react and @types/react-dom as dev dependencies. For Next.js projects, React is automatically installed and managed by Next.js itself, so this error is less common but can still occur if dependencies are manually modified. If you encounter this error in a Docker container, ensure your Dockerfile includes the npm install step after copying package.json and package-lock.json. For projects using module resolution path mapping (like TypeScript paths or webpack aliases), verify that your configuration does not interfere with resolving the react package from node_modules. If you are migrating from an older React version, check for breaking changes in the React changelog that might affect your dependencies. The "Can't resolve" message is generic and applies to any missing module, so these troubleshooting steps work for other packages as well.
Prop spreading could cause security issues
Prop spreading could cause security issues
Error: error:0308010C:digital envelope routines::unsupported
Error: error:0308010C:digital envelope routines::unsupported
React Hook "useEffect" is called conditionally. React Hooks must be called in the exact same order in every component render.
React Hook useEffect placed inside a condition
Hook can only be called inside the body of a function component
Hook can only be called inside the body of a function component
Rollup failed to resolve import during build
How to fix "Rollup failed to resolve import" in React