Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Webpack warns that React was not found in the react package

Errorexport 'React' (imported as 'React') was not found in 'react'

Short answer

This happens when the code writes import { React } from 'react'. The react package has no export named React, so the bundler cannot find it. Import the default or the whole namespace instead, or better, enable the automatic JSX runtime and remove the React import from files that only use JSX.

  • Applies to React 19, webpack 5, Babel 7, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every import that pulls React in braces.

    grep -rn "{ *React" src/
  2. 2Match what you see, then jump to its fix

    1. 1The failing file contains import { React } from 'react'React is imported as a named exportFix
    2. 2Several files import React only because JSX is used, and removing it gives 'React is not defined'The import was added to satisfy the old JSX transformFix

What does the error look like?

  • export 'React' (imported as 'React') was not found in 'react'

Cause 1 of 2

React is imported as a named export

1Check

React's guide says a named export must be imported with braces and a matching name, and a default export without braces. React's JSX transform article shows named imports only for things such as hooks, and a namespace import written as import * as React. The word React itself is not a named export of the package.

grep -rn "import {.*React.*} from 'react'" src/

2Fix

Use the default or namespace import, and keep braces for hooks and other real exports.

import * as React from 'react';
import { useState } from 'react';

3Verify

Run the build; the warning for that file is gone.

npm run build

Confirmed by legacy.reactjs.org

Did this fix it?

Cause 2 of 2

The import was added to satisfy the old JSX transform

1Check

With the classic runtime, JSX becomes React.createElement calls, so React must be in scope. The new transform inserts its own import from react/jsx-runtime. It is supported by Babel 7.9 and later, TypeScript 4.1 and later, and Create React App 4 and Next.js 9.5.3 and later.

grep -n "runtime" babel.config.json .babelrc 2>/dev/null

2Fix

Turn on the automatic runtime in Babel, then run the codemod that removes unused React imports.

// babel.config.json
{ "presets": [["@babel/preset-react", { "runtime": "automatic" }]] }

# remove unneeded imports
npx react-codemod update-react-imports

3Verify

Build again and check that files with only JSX no longer need an import.

npm run build

Confirmed by legacy.reactjs.org

Did this fix it?

How do I stop it coming back?

  • Import only hooks and APIs you use, by name; leave JSX files without a React import.
  • Turn off react/react-in-jsx-scope in ESLint when the automatic runtime is on.
  • Run the build in CI so import warnings are seen before release.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.