TypeError: Cannot read property 'appendChild' of Null in JS

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

TypeError: Cannot read property 'appendChild' of Null in JS #

The "Cannot read property 'appendChild' of null" error occurs for 2 reasons:

  1. Calling the appendChild() method on a DOM element that doesn't exist, e.g calling getElementById with an invalid id.
  2. Inserting the JS script tag above the HTML, where the DOM elements are declared.

cannot read property appendchild of null

Here is an example of how the error occurs.

index.js
const el = null; const p = document.createElement('p'); // ⛔️ Uncaught TypeError: Cannot read properties of null (reading 'appendChild') el.appendChild(p);

To solve the "Cannot read property 'appendChild' of null" error, make sure the DOM element on which you're calling the method exists. The error often occurs after providing an invalid id to the getElementById method.

index.js
const el = document.getElementById('does-not-exist'); console.log(el); // 👉️ null const p = document.createElement('p'); // ⛔️ Cannot read properties of null (reading 'appendChild') el.appendChild(p);
We called the getElementById() method with an id that doesn't exist in the DOM, so we got a null value. Calling the appendChild method on a null value is what causes the error.

To solve the "Cannot read property 'appendChild' of null" error, make sure to insert the JS script tag at the bottom of the body. The JS script tag should be placed after the HTML elements have been declared.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <!-- ⛔️ BAD - script is run before HTML is declared ⛔️ --> <script src="index.js"></script> <div id="box"></div> </body> </html>
We placed the JS script tag above the div element, so the index.js file is run before the DOM element is created.

This means that we are unable to access the div in the index.js file.

index.js
const el = document.getElementById('box'); console.log(el); // 👉️ null const p = document.createElement('p'); // ⛔️ Cannot read properties of null (reading 'appendChild') el.appendChild(p);

You have to place the JS script tag at the bottom of the body, after the DOM elements have been declared.

index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <div id="box"></div> <!-- ✅ GOOD - HTML is already available ✅ --> <script src="index.js"></script> </body> </html>

Now you can access the div element inside of the index.js file.

index.js
const el = document.getElementById('box'); console.log(el); // 👉️ div const p = document.createElement('p'); // ✅ works el.appendChild(p);

Conclusion #

The "Cannot read property 'appendChild' of null" error occurs when trying to call the appendChild method on a DOM element that doesn't exist.

To solve the error, make sure to only call the appendChild method on valid DOM elements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.