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.
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.
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.
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.
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