日本語

Fixing the “older version of React” error when two React copies are installed

​​React​npm,Troubleshooting,TypeScript,Vite

In an app upgraded to React 19, or in a new React 19 app, installing a UI component library from npm can leave the page blank and show A React Element from an older version of React was rendered. This error happens when the app contains both React 19 and React 18, and the library's components were created with the older React 18. The error goes away when the library uses the same single copy of React as the app.

In my test environment, I built a library that listed React 18 in dependencies, installed it in an app, and triggered this error. I then fixed it from both the app side and the library side and confirmed that the error disappeared.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and npm 10.9.9. The app was created with the react-ts template of npm create vite@latest (react and react-dom 19.3.1, Vite 8.4.0, TypeScript 7.0.2). I installed my own library from a tgz file created with npm pack. I used esbuild 0.25.12 to build the library. I checked the pages in Chromium 158 using Playwright 1.63.0. I did not test frameworks such as Next.js, webpack, pnpm or yarn, libraries linked with npm link, or the third cause in the error message (a compiler that inlines JSX instead of going through the runtime).

The full error in the console

When I opened the page with the dev server (vite, the same command as npm run dev), nothing appeared on the screen, and the console showed this error:

text
A React Element from an older version of React was rendered. This is not supported. It can happen if:
- Multiple copies of the "react" package is used.
- A library pre-bundled an old copy of "react" or "react/jsx-runtime".
- A compiler tries to "inline" JSX instead of using the runtime.

A warning followed, naming the component where the error happened. <Card> is a component that I imported from the library.

text
An error occurred in the <Card> component. Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://react.dev/link/error-boundaries to learn more about error boundaries.
On the DevTools console, a red line reads "Uncaught Error: A React Element from an older version of React was rendered." followed by the three possible causes. Below it, a yellow line reads "An error occurred in the <Card> component." The page above shows nothing.
The page opened in the dev server after installing a library that depends on React 18

The error message means "A React Element created by an older version of React was rendered. This is not supported." It lists three possible causes: multiple copies of the react package are installed; a library has bundled (inlined) an old copy of react or react/jsx-runtime inside itself; or a compiler converts JSX into a form that does not go through the runtime. In this article, I reproduced the first two.

In a production build, the same error appeared only as a number:

text
Minified React error #525; visit https://react.dev/errors/525 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.

In React's error code table, code 525 is the error message in this article. If you see #525 on a live site, you can check it with the steps in this article.

To see the full message in a built page, I created a development build with a command like NODE_ENV=development npx vite build --mode development --minify false --outDir dist-dev, then opened it with npx vite preview --outDir dist-dev. In this article, "development build" refers to the output of this command. When I opened it, the error showed the same full message as the dev server.

React 18 and React 19 use different $$typeof values for React elements

During the build, JSX is converted into code that calls jsx() from react/jsx-runtime, and jsx() returns a React element object. This object has a $$typeof property that marks it as a React element, and the value of that property differs between React 18 and React 19. Here are the relevant lines from the two versions installed in the test environment:

js
// react 19.3.1 react/cjs/react-jsx-runtime.production.js (one line excerpted)
var REACT_ELEMENT_TYPE = Symbol.for("react.transitional.element"),

// react 18.3.1 react/cjs/react-jsx-runtime.development.js (one line excerpted)
var REACT_ELEMENT_TYPE = Symbol.for('react.element');

react-dom 19.3.1 throws this error when the element it tries to render has a $$typeof of React 18's Symbol.for("react.element"). In the following code, I have omitted the rest of the error message.

js
// react-dom 19.3.1 react-dom/cjs/react-dom-client.development.js
function throwOnInvalidObjectTypeImpl(returnFiber, newChild) {
  if (newChild.$$typeof === REACT_LEGACY_ELEMENT_TYPE)
    throw Error(
      'A React Element from an older version of React was rendered. ...'

In the same file, REACT_LEGACY_ELEMENT_TYPE is defined as Symbol.for("react.element"). In other words, if a library component creates elements with React 18's jsx(), React 19's react-dom cannot render them.

Reproducing the error with a library that depends on React 18

For the test, I made a library called old-card with one component, Card. It was distributed as built JavaScript and imported react/jsx-runtime. I listed React 18 in its dependencies in package.json:

json
{
  "name": "old-card",
  "version": "1.0.0",
  "type": "module",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": ["dist"],
  "dependencies": {
    "react": "^18.3.1"
  }
}
js
// Built library (JSX is already converted to calls to react/jsx-runtime)
import { jsx, jsxs } from 'react/jsx-runtime';

export function Card({ title, children }) {
  return jsxs('section', {
    className: 'card',
    children: [jsx('h2', { children: title }), children],
  });
}

To match what happens when a package is installed from npm, I packed it into a tgz file with npm pack and installed it in the app with npm i ../old-card/old-card-1.0.0.tgz. The app's App.tsx was:

tsx
import { Card } from 'old-card'

export default function App() {
  return (
    <main>
      <h1>お知らせ</h1>
      <Card title="メンテナンスのお知らせ">
        <p>12月1日の夜に、サービスを一時停止します。</p>
      </Card>
    </main>
  )
}

The type check with npx tsc -b passed. The error appeared only when I opened the page. The screen was blank whether I used the dev server, a production build, or a development build.

Checking how many Reacts are installed with npm ls react

I ran npm ls react in the app folder. Under old-card, there was a second React, version 18 (I replaced the paths with example values):

text
app@0.0.0 /path/to/app
+-- old-card@1.0.0
| `-- react@18.3.1
+-- react-dom@19.3.1
| `-- react@19.3.1 deduped
`-- react@19.3.1

The app requires React 19 and old-card requires React 18, so npm installs React 18 separately in node_modules/old-card/node_modules/react. The import ... from 'react/jsx-runtime' inside old-card points to this React 18.

The dev server also used this React 18. In node_modules/.vite/deps/old-card.js, which Vite pre-bundled, the react-jsx-runtime.development.js from node_modules/old-card/node_modules/react had been included.

I tried three ways to fix the case where two copies of React are installed: setting it in the app's package.json, fixing the library's package.json, and setting it in the Vite config. Each one removed the error in all three builds (dev server, production build, and development build). The notice text and the Card content were displayed.

Using overrides in the app's package.json to keep one React

If you can't modify the library, you can add overrides to the app's package.json so that the react used by the library is set to the same version as the app. Add overrides at the top level of package.json, as shown below. "$react" means "use the same spec as react in the app's dependencies."

json

"overrides": {
    "old-card": {
      "react": "$react"
    }
  }

In my test, writing this setting and running npm i alone did not remove the React 18 that was already installed. npm ls react showed invalid and exited with an error (exit code 1). Part of the output is shown below:

text
+-- old-card@1.0.0 overridden
| `-- react@18.3.1 invalid: "^19.3.1" from node_modules/old-card overridden

Deleting only node_modules and running npm i again gave the same result. After I deleted both node_modules and package-lock.json and ran npm i again, React became a single copy (the first line is omitted here):

text
+-- old-card@1.0.0 overridden
| `-- react@19.3.1 deduped
+-- react-dom@19.3.1
| `-- react@19.3.1 deduped
`-- react@19.3.1

According to npm's documentation for package-lock.json, the file records the dependency tree that was created so that the same tree can be created the next time. If you delete it, other packages are also re-resolved without their recorded versions. So commit it before deleting it, and check the diff after reinstalling.

Declaring react as a peerDependency in the library

If you maintain the library yourself, list react under peerDependencies instead of dependencies. This means "this library works with the React that the app installs." npm then does not install a separate React under the library.

json
{
  "name": "old-card",
  "version": "1.1.0",
  "type": "module",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": [
    "dist"
  ],
  "peerDependencies": {
    "react": "^18.3.1 || ^19.0.0"
  }
}

When I installed the fixed version 1.1.0 (with the overrides removed from the app), npm ls react showed react@19.3.1 deduped under old-card@1.1.0, and the error did not appear. As the npm documentation says, peerDependencies have been installed automatically since npm 7.

Using Vite's resolve.dedupe to keep one React for Vite's bundles

If you use Vite, you can also add resolve.dedupe to vite.config.ts. It makes the specified packages resolve to the copy in the project root, no matter where they are imported from.

ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vite.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    // Use the react in the project root, no matter where it is imported from
    dedupe: ['react', 'react-dom'],
  },
})

I reverted the library to old-card@1.0.0 and added this setting while npm ls react still showed React 18 under old-card. The error no longer appeared in the dev server or in the build. In the development build, the only React included was node_modules/react in the app root.

The page shows a notice heading, a card titled "Maintenance notice" and the text "Service will be paused on the night of December 1." Below, the DevTools console shows only one line of React DevTools guidance.
The page reopened in the dev server after adding resolve.dedupe. No error appears

However, this only merges the copies inside what Vite builds. Two copies of React still remain in node_modules. If you want to remove React 18 from the npm ls react output, fix it with overrides or peerDependencies.

When npm ls shows only React 19 but the error still appears

The second cause in the error message is that the library has bundled an old react/jsx-runtime. In this case, npm ls react does not show React 18. In my test, I built the library bundled-card with esbuild using --bundle, which included React 18's react/jsx-runtime in the library's dist/index.js:

bash
npx esbuild src/index.jsx --bundle --format=esm --jsx=automatic --minify --define:process.env.NODE_ENV='"production"' --outfile=dist/index.js

The resulting dist/index.js had no import ... from "react" at all. Instead, it contained Symbol.for("react.element"). I removed old-card from the app, installed this bundled-card, and changed the import in App.tsx to bundled-card. The npm ls react output was as follows. The only React installed is 19.3.1 (I replaced the paths with example values):

text
app@0.0.0 /path/to/app
+-- bundled-card@1.0.0
| `-- react@19.3.1 deduped
+-- react-dom@19.3.1
| `-- react@19.3.1 deduped
`-- react@19.3.1

Even so, the dev server showed the full error message, and the production build showed #525. The error did not go away even with the resolve.dedupe setting from earlier.

In this case, the component name in the warning was not <Card>. It was An error occurred in the <we> component. In the dist/index.js that --minify had shortened, the component function was function we({title:e,children:t}), and export{we as Card} exposed it under the name Card. The warning shows the shortened name.

What fixed it was changing how the library is built. I rebuilt with --external:react:

bash
npx esbuild src/index.jsx --bundle --format=esm --jsx=automatic --external:react --outfile=dist/index.js

The resulting dist/index.js is shown below. The react/jsx-runtime import was left as is, not bundled in. (The comment on the first line is the original file name that esbuild added.)

js
// src/index.jsx
import { jsx, jsxs } from "react/jsx-runtime";
function Card({ title, children }) {
  return /* @__PURE__ */ jsxs("section", { className: "card", children: [
    /* @__PURE__ */ jsx("h2", { children: title }),
    children
  ] });
}
export {
  Card
};

When I installed this version (1.0.1) in the app again, no error appeared in either the dev server or the production build, and the Card content was displayed. Settings on the app side, such as resolve.dedupe, did not fix it. If you do not own the library, include the output of npm ls react and the fact that Symbol.for("react.element") appears in the library's dist when you report the problem to the library's author.

​Posted by 86