Accepted answer

i figured it out!

after reading this blog post i realized that the placement of this line:

<script src="{% static "build/react.js" %}"></script>

was wrong. that line needs to be the last line in the <body> section, right before the </body> tag. moving the line down solves the problem.

my explanation for this is that react was looking for the id in between the <head> tags, instead of in the <body> tags. because of this it couldn't find the content id, and thus it wasn't a real dom element.


target container is not a dom element.

i achieved this error with a simple starter app also.

// index.js

      <app />,


syntax errors can cause this error. i checked my syntax and wrapped my <app /> properly.

      <app />


i got the same error i created the app with create-react-app but in /public/index.html also added matrialize script but there was to connection with "root" so i added

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

just before

<script src=" materialize.min.js"></script>

and it worked for me .


one of the case i encountered the same error in a simple project. i hope the solution helps someone.

below code snippets are sufficient to understand the solution :


    <noscript>you need to enable javascript to run this app.</noscript>
    <div id="root"></div>

somefile.js : notice the line const portalelement = document.getelementbyid("overlays"); below :

const portalelement = document.getelementbyid("overlays");

const modal = (props) => {
  return (
            {reactdom.createportal(<backdrop />, portalelement)}

i didn't have any element with id = "overlays" in my index.html file, so the highlighted line above was outputting null and so react wasn't able to find inside which element it should create the portal i.e {reactdom.createportal(<backdrop />, portalelement)} so i got below error

enter image description here

i added the div in index.html file and the error was gone.

    <noscript>you need to enable javascript to run this app.</noscript>
    <div id="overlays"></div>
    <div id="root"></div>


also you can do something like that:

document.addeventlistener("domcontentloaded", function(event) {
    cardbox({url: "/cards/?format=json", pollinterval: 2000}),

the domcontentloaded event fires when the initial html document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.


for those that implemented react js in some part of the website and encounter this issue. just add a condition to check if the element exist on that page before you render the react component.

<div id="element"></div>


const someelement = document.getelementbyid("element")
if(someelement) {
  reactdom.render(<yourcomponent />, someelement)


i had encountered the same error with react version 16. this error comes when the javascript that tries to render the react component is included before the static parent dom element in the html. fix is same as the accepted answer, i.e. the javascript should get included only after the static parent dom element has been defined in the html.


also, the best practice of moving your <script></script> to the bottom of the html file fixes this too.


just to give an alternative solution, because it isn't mentioned.

it's perfectly fine to use the html attribute defer here. so when loading the dom, a regular <script> will load when the dom hits the script. but if we use defer, then the dom and the script will load in parallel. the cool thing is the script gets evaluated in the end - when the dom has loaded (source).

<script src="{% static "build/react.js" %}" defer></script>


webpack solution

if you got this error while working in react with webpack and hmr.

you need to create template index.html and save it in src folder:

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

now when we have template with id="root" we need to tell webpack to generate index.html which will mirror our index.html file.

to do that:

plugins: [
    new htmlwebpackplugin({
        title: "application name",
        template: './src/index.html'

template property will tell webpack how to build index.html file.


also make sure id set in index.html is same as the one you referring to in index.js


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



Related Query

More Query from same tag