Maaz Zindani

Loading the index…

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

createRoot fails with Target container is not a DOM element

ErrorError: Target container is not a DOM element.

Short answer

createRoot needs a real DOM node. If the argument is null or a React element, React throws this error. Log the value you pass in. A null result means getElementById found nothing: the id differs, or your script runs before the element exists in the HTML. A component passed in place of the node is the other cause.

  • Applies to React 19, react-dom 19, webpack 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the value passed to createRoot. null means no element was found; anything else means it is not a node.

    const domNode = document.getElementById('root');
    console.log(domNode);
    const root = createRoot(domNode);
  2. 2Match what you see, then jump to its fix

    1. 1console.log prints nullThe id in the code differs from the id in the HTMLFix
    2. 2The id matches, but console.log still prints nullThe script runs before the element is in the pageFix
    3. 3console.log prints a React element or a functionA component was passed instead of the nodeFix

What does the error look like?

  • Error: Target container is not a DOM element.
  • Uncaught Error: Target container is not a DOM element

Cause 1 of 3

The id in the code differs from the id in the HTML

1Check

Compare the id string in getElementById with the id attribute in your HTML file or template, letter by letter.

grep -rn "id=\"root\"\|getElementById" public src index.html

2Fix

Make the two match.

<div id="root"></div>

const domNode = document.getElementById('root');

3Verify

Reload; the console should now print the element, not null.

console.log(document.getElementById('root'))

Confirmed by react.dev

Did this fix it?

Cause 2 of 3

The script runs before the element is in the page

1Check

A script cannot see nodes that come after its tag in the HTML. Check where the bundle's script tag sits compared with the root div. This often happens in Django or other server templates.

grep -n "<script\|id=\"root\"" templates/index.html

2Fix

Put the script tag after the root element, at the end of the body.

<body>
  <div id="root"></div>
  <script src="/static/bundle.js"></script>
</body>

3Verify

Reload the page and check the console.

console.log(document.getElementById('root'))

Confirmed by react.dev

Did this fix it?

Cause 3 of 3

A component was passed instead of the node

1Check

Look at the createRoot call. The component belongs in render, not in createRoot.

grep -rn "createRoot(" src

2Fix

Pass the DOM node to createRoot and the component to render.

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

3Verify

Reload the page.

npm start

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Keep the root id in one place and reuse it in the template and the entry file.
  • Load the bundle at the end of the body, or use a script tag with defer.
  • Render a different root div per React app when one page holds several.

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

Next step

Want this handled for good?

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