Maaz Zindani

Loading the index…

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

createRoot from react-dom/client cannot be imported after upgrading to React 18

ErrorTypeScript cannot find the module react-dom/client

Short answer

createRoot only exists in react-dom 18 and newer, and it is imported from react-dom/client, not react-dom. Check that react and react-dom are both on 18, switch the import, and update @types/react and @types/react-dom so TypeScript knows the new entry point.

  • Applies to React 18, React 19, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the installed versions of both packages.

    npm ls react react-dom @types/react @types/react-dom
  2. 2Match what you see, then jump to its fix

    1. 1npm ls shows react 18 but react-dom 17, or the reversereact-dom is still on an old versionFix
    2. 2React 18 logs an error and runs in React 17 modeThe app still uses ReactDOM.renderFix
    3. 3TypeScript cannot find types for react-dom/client, or children errors appear on componentsThe TypeScript type packages are oldFix

What does the error look like?

  • TypeScript cannot find the module react-dom/client
  • createRoot cannot be imported from react-dom
  • The app still runs in React 17 mode after upgrading

Cause 1 of 3

react-dom is still on an old version

1Check

Both packages must be upgraded together.

npm ls react react-dom

2Fix

Install both again.

npm install react react-dom

3Verify

npm ls shows the same major version for both.

npm ls react react-dom

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

The app still uses ReactDOM.render

1Check

Without switching to createRoot, React 18 runs the app in React 17 mode.

grep -rn "ReactDOM.render\|from 'react-dom'" src

2Fix

Import createRoot from react-dom/client and render through the root.

import { createRoot } from 'react-dom/client';

const container = document.getElementById('app');
const root = createRoot(container);
root.render(<App />);

3Verify

Start the app and check that the console error is gone.

npm run dev

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

The TypeScript type packages are old

1Check

The type packages must be updated along with React. In the newer types, children is no longer implied and must be declared in props.

npm ls @types/react @types/react-dom

2Fix

Update both type packages, and add children to props where a component uses it. With TypeScript, getElementById returns a nullable value, so pass the container with a non-null assertion or check it first.

npm install --save-dev @types/react@latest @types/react-dom@latest

interface MyButtonProps {
  color: string;
  children?: React.ReactNode;
}

3Verify

Run the type checker.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Upgrade react, react-dom and both type packages in one commit.
  • Run the upgrade guide's codemods where it offers them.
  • Check that libraries support your React version before upgrading.

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.