This error occurs when TypeScript cannot locate the React module during compilation. It typically happens because React and its type definitions are not installed, or the moduleResolution setting in tsconfig.json is misconfigured.
The 'Cannot find module react' error indicates that TypeScript's module resolution system failed to locate the React package in your node_modules directory. This error can occur even if React is installed but the corresponding TypeScript type definitions (@types/react) are missing. TypeScript requires both the runtime package and type definitions to provide type safety and IntelliSense for third-party libraries. When either component is missing or misconfigured, TypeScript cannot understand the React module structure and throws this error during compilation or when your IDE performs type checking.
Install or reinstall React along with its TypeScript type definitions:
npm install react react-dom
npm install -D @types/react @types/react-domFor yarn users:
yarn add react react-dom
yarn add -D @types/react @types/react-domFor pnpm users:
pnpm add react react-dom
pnpm add -D @types/react @types/react-domVerify the installation by checking your package.json:
{
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.0"
}
}Ensure your tsconfig.json has the correct moduleResolution setting. Use 'node' for most React projects:
{
"compilerOptions": {
"moduleResolution": "node",
"module": "esnext",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"jsx": "react-jsx",
"esModuleInterop": true,
"skipLibCheck": false
}
}For modern Node.js projects (Node 16+), you can use:
{
"compilerOptions": {
"moduleResolution": "node16"
}
}If the error persists after installing React, clean your node_modules and lockfile, then reinstall:
# For npm
rm -rf node_modules package-lock.json
npm install
# For yarn
rm -rf node_modules yarn.lock
yarn install
# For pnpm
rm -rf node_modules pnpm-lock.yaml
pnpm installAfter reinstalling, restart your TypeScript language server:
- VS Code: Press Ctrl+Shift+P (Cmd+Shift+P on Mac), then select 'TypeScript: Restart TS server'
- WebStorm: File > Invalidate Caches / Restart
Check that your TypeScript version is compatible with your React types. React 18 type definitions require TypeScript 4.5 or higher:
# Check TypeScript version
npx tsc --version
# Check installed React types version
npm list @types/reactIf your TypeScript version is outdated, upgrade it:
npm install -D typescript@latestFor Create React App projects, ensure you have the correct TypeScript setup:
npm install --save typescript @types/node @types/react @types/react-dom @types/jestReact 18 and newer require Node.js 14 or higher. Verify your Node.js version:
node --versionIf you're using an older version, upgrade Node.js using nvm (recommended):
# Install nvm first if not installed
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Install and use Node.js LTS
nvm install --lts
nvm use --ltsAfter upgrading Node.js, reinstall your dependencies:
rm -rf node_modules package-lock.json
npm installIf the issue persists, use TypeScript's module resolution tracing to diagnose the problem:
npx tsc --traceResolution --noEmit | grep -i reactThis will show you exactly where TypeScript is looking for the React module and why it's failing. Common issues revealed by tracing:
- TypeScript is looking in the wrong directory
- The moduleResolution strategy is not finding node_modules
- Type definitions are installed but in an unexpected location
You can also check if TypeScript can find React at all:
npx tsc --showConfigType Definition Versions: The @types/react package should match your React major version. React 18.x should use @types/[email protected]. Mismatched versions can cause subtle type errors even if the module resolves. skipLibCheck Workaround: Setting skipLibCheck to true in tsconfig.json can mask this error but is not recommended as it skips type checking for all declaration files, potentially hiding other issues. Monorepo Setup: In monorepo environments using workspaces, ensure that React is installed at the correct level (root vs. package) and that TypeScript can resolve cross-package dependencies. Use the references field in tsconfig.json for project references. Next.js Specific: Next.js projects automatically include React type definitions, but if you're setting up a custom TypeScript configuration, ensure you don't override the built-in settings. Next.js uses moduleResolution: 'bundler' by default in newer versions. CI/CD Environments: In continuous integration pipelines, this error often occurs when node_modules is not cached properly or when the install step fails silently. Always verify that npm install or yarn install completes successfully before running TypeScript compilation.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript