How to fix Cannot find module 'react' in React
The "Cannot find module 'react'" error occurs when Node.js or your bundler cannot locate the React package in your project dependencies. This typically happens when React is not installed, when package.json is misconfigured, or when dependencies were not properly resolved.
Quick fix
Fixes Cannot find module 'react'
{
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}- Verify React is listed in package.json
- Install dependencies if node_modules is missing
- Clear package manager cache and reinstall
- Verify you're running from the correct directory
{"dependencies": {"react": "^18.2.0","react-dom": "^18.2.0"}}Cannot find module 'react'
What this error means
The "Cannot find module 'react'" error is thrown by Node.js when it attempts to resolve the react import statement but cannot find the package in the expected locations. This can occur in several contexts: during runtime in Node.js scripts, during build time in your bundler (webpack, vite, etc.), or during development when running tests or scripts. React is not a built-in Node.js module, so it must be explicitly installed as a dependency in your project. When Node.js encounters an import like `import React from 'react'` or `const React = require('react')`, it searches in node_modules and follows the module resolution algorithm. If React is missing from node_modules, your package manager hasn't been run, or the package.json doesn't declare React as a dependency, the module cannot be found. This error is common when setting up new React projects, cloning repositories without running dependency installation, or when using Node.js to run scripts in non-browser environments where React wasn't listed as a dependency.
Typical symptoms
- Error message in console: "Cannot find module 'react'"
- Build process fails when compiling JSX files
- Development server won't start or crashes on startup
- npm start or yarn start command produces the error
- Test runner (Jest, Vitest, etc.) fails when importing React components
- Server-side rendering (SSR) scripts fail with module not found error
- Editor shows red squiggle on React imports despite project seeming correct
Common causes
- React package is not installed in the project (node_modules missing or empty)
- package.json does not list React in dependencies, devDependencies, or peerDependencies
- Running npm install or yarn install has not been executed after cloning a repository
- Incorrect NODE_PATH configuration or module resolution settings in build config
- package-lock.json or yarn.lock is corrupted or out of sync with package.json
- Node modules were deleted or corrupted and need to be reinstalled
- Using a different package manager (e.g., yarn) than what is specified by lock file (package-lock.json)
- Running Node.js from a directory outside the project root where node_modules is not accessible
How to fix "Cannot find module 'react'"
Check that your package.json includes React in the dependencies. The dependencies section should look similar to:
{
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
}If React is not listed, add it by installing:
npm install react react-dom
# or
yarn add react react-dom
# or
pnpm add react react-domReact typically comes with react-dom for rendering to the DOM, so install both together.
The most common cause is that node_modules was deleted or never created. Install all dependencies:
npm install
# or
yarn install
# or
pnpm installThis reads your package.json and package-lock.json (or yarn.lock/pnpm-lock.yaml) to download and install all declared dependencies, including React.
For a fresh clone of a repository, always run the install command before attempting to run the project:
git clone https://github.com/user/repo.git
cd repo
npm install # Required before npm start
npm startSometimes package manager caches become corrupted. Clear the cache and perform a clean reinstall:
# npm
npm cache clean --force
rm -rf node_modules package-lock.json
npm install
# yarn
yarn cache clean
rm -rf node_modules yarn.lock
yarn install
# pnpm
pnpm store prune
rm -rf node_modules pnpm-lock.yaml
pnpm installThis ensures you get a fresh download of all packages from the registry.
The module resolution looks for node_modules in the current directory and parent directories. Make sure you're running your command from the project root where package.json exists:
# Check current directory
pwd
# List files to confirm package.json exists here
ls package.json
# If you're in a subdirectory, move to the project root
cd /path/to/project/root
# Now run your command
npm startIf you're using a monorepo with workspaces (Lerna, yarn workspaces, pnpm workspaces), ensure node_modules exists at the root level or in the workspace root.
Verify that the Node.js version being used has access to the correct node_modules:
# Check which Node.js is being used
which node
node --version
# Check where npm is looking for modules
npm root
# Verify node_modules exists at that location
ls -la $(npm root)/../react
# Check NODE_PATH environment variable (if set)
echo $NODE_PATHIf NODE_PATH is set, it may be pointing to the wrong directory. You can temporarily unset it:
unset NODE_PATH
npm startIn most modern Node.js projects, NODE_PATH should not be needed—npm's default module resolution should work.
If you're using a custom bundler configuration (webpack, Vite, Rollup, etc.), ensure it's configured to resolve node_modules correctly.
For webpack, verify webpack.config.js includes:
module.exports = {
resolve: {
modules: ['node_modules'],
extensions: ['.js', '.jsx', '.ts', '.tsx']
}
};For Vite, the default config should work, but check vite.config.js:
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [react()]
});For Next.js, the configuration is automatic, but ensure next.config.js doesn't have conflicting resolver settings.
If you're in a monorepo using npm/yarn/pnpm workspaces, ensure dependencies are properly installed at the workspace root:
# For npm workspaces
npm install -w ./packages/my-app
# For yarn workspaces
yarn install
# For pnpm workspaces
pnpm installVerify the workspace configuration in your package.json:
{
"workspaces": [
"packages/*"
]
}Ensure each package in the workspace that uses React has it listed as a dependency or peerDependency. If React is only in the root, child packages may not be able to resolve it depending on hoisting settings.
For pnpm, which doesn't hoist by default, use:
# pnpm-workspace.yaml
packages:
- 'packages/*'
pnpm:
hoistPattern:
- '*'If standard module resolution continues to fail, you can sometimes work around it using exact file paths, though this is a temporary workaround only:
// ❌ Standard import (fails if React not found)
import React from 'react';
// Temporary workaround - absolute path (use cautiously)
import React from '/full/path/to/node_modules/react/index.js';However, this is NOT recommended for production code. Always resolve the underlying issue by ensuring React is properly installed. This workaround is only useful for debugging to confirm the package file itself is intact.
Advanced notes
Docker and Container Environments: When running Node.js in Docker, ensure npm install is executed during the container build, not just on the host. Include it in your Dockerfile before running your application:
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]CI/CD Pipelines: When deploying through GitHub Actions, GitLab CI, or similar tools, ensure the dependency installation step runs before building or testing. Check that your workflow runs npm install, yarn install, or pnpm install before running scripts.
Version Managers (nvm, fnm): If using multiple Node.js versions through nvm or fnm, verify the active version has the correct node_modules installed. Different Node.js versions can have separate node_modules caches:
nvm use 18.12.0
npm installSymlinks and npm link: If you're using npm link for local package development, ensure the linked package's node_modules is properly set up. Symlinks may break if the source package doesn't have React installed.
Yarn PnP (Plug'n'Play): If using Yarn with plugnPlayEnabled: true, module resolution works differently. Yarn PnP doesn't use node_modules, so standard module resolution doesn't apply. Ensure your bundler and tools are configured for PnP mode.
Frequently asked questions
- What does "Cannot find module 'react'" mean?
- js when it attempts to resolve the react import statement but cannot find the package in the expected locations. ), or during development when running tests or scripts. js module, so it must be explicitly installed as a dependency in your project.
- How do I fix "Cannot find module 'react'"?
- The "Cannot find module 'react'" error occurs when Node.js or your bundler cannot locate the React package in your project dependencies. This typically happens when React is not installed, when package.json is misconfigured, or when dependencies were not properly resolved.
- What causes "Cannot find module 'react'"?
- React package is not installed in the project (node_modules missing or empty) package.json does not list React in dependencies, devDependencies, or peerDependencies Running npm install or yarn install has not been executed after cloning a repository
- Is the "Cannot find module 'react'" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚React Official Documentation
- 📖Node.js Guides: Module Basics(Official Docs)
- 📖npm Docs: npm install command(Official Docs)
- 📖Node.js Docs: Module Resolution(Official Docs)
- 📖React Official Docs: Getting Started(Official Docs)
- 🐙
- 💬Stack Overflow: Cannot find module react(Stack Overflow)