How to fix Cannot find module 'react' in React

ReactBEGINNERHIGHUpdated Dec 9, 2025· 113 views

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"
  }
}
  1. Verify React is listed in package.json
  2. Install dependencies if node_modules is missing
  3. Clear package manager cache and reinstall
  4. Verify you're running from the correct directory
react — error
{
"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

  1. React package is not installed in the project (node_modules missing or empty)
  2. package.json does not list React in dependencies, devDependencies, or peerDependencies
  3. Running npm install or yarn install has not been executed after cloning a repository
  4. Incorrect NODE_PATH configuration or module resolution settings in build config
  5. package-lock.json or yarn.lock is corrupted or out of sync with package.json
  6. Node modules were deleted or corrupted and need to be reinstalled
  7. Using a different package manager (e.g., yarn) than what is specified by lock file (package-lock.json)
  8. Running Node.js from a directory outside the project root where node_modules is not accessible

How to fix "Cannot find module 'react'"

1Verify React is listed in package.json

Check that your package.json includes React in the dependencies. The dependencies section should look similar to:

json
{
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
}

If React is not listed, add it by installing:

bash
npm install react react-dom
# or
yarn add react react-dom
# or
pnpm add react react-dom

React typically comes with react-dom for rendering to the DOM, so install both together.

2Install dependencies if node_modules is missing

The most common cause is that node_modules was deleted or never created. Install all dependencies:

bash
npm install
# or
yarn install
# or
pnpm install

This 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:

bash
git clone https://github.com/user/repo.git
cd repo
npm install  # Required before npm start
npm start
3Clear package manager cache and reinstall

Sometimes package manager caches become corrupted. Clear the cache and perform a clean reinstall:

bash
# 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 install

This ensures you get a fresh download of all packages from the registry.

4Verify you're running from the correct directory

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:

bash
# 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 start

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

5Check for Node.js PATH and NODE_PATH issues

Verify that the Node.js version being used has access to the correct node_modules:

bash
# 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_PATH

If NODE_PATH is set, it may be pointing to the wrong directory. You can temporarily unset it:

bash
unset NODE_PATH
npm start

In most modern Node.js projects, NODE_PATH should not be needed—npm's default module resolution should work.

6Verify build configuration includes React resolver

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:

javascript
module.exports = {
  resolve: {
    modules: ['node_modules'],
    extensions: ['.js', '.jsx', '.ts', '.tsx']
  }
};

For Vite, the default config should work, but check vite.config.js:

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

7Check for monorepo and workspace issues

If you're in a monorepo using npm/yarn/pnpm workspaces, ensure dependencies are properly installed at the workspace root:

bash
# For npm workspaces
npm install -w ./packages/my-app

# For yarn workspaces
yarn install

# For pnpm workspaces
pnpm install

Verify the workspace configuration in your package.json:

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:

yaml
# pnpm-workspace.yaml
packages:
  - 'packages/*'

pnpm:
  hoistPattern:
    - '*'
8Use exact import paths when module resolution fails

If standard module resolution continues to fail, you can sometimes work around it using exact file paths, though this is a temporary workaround only:

javascript
// ❌ 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:

dockerfile
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:

bash
nvm use 18.12.0
npm install

Symlinks 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