Solution 1 :

Most probably it is trying to reach body before DOM is ready. You should use DOMContentLoaded event for DOM manipulation.

window.addEventListener('DOMContentLoaded', (event) => {

Otherwise, it is better to share more code.

Problem :

Whenever I try to use document.body.appendChild, it evaluates to null. Here is my code:


I get this error:

main.js:96 Uncaught TypeError: Cannot read property 'appendChild' of null


Comment posted by Teemu

Your script is executed in the head section while the page is parsed, at that time

Comment posted by Teemu

Yes, that’s one of the many possible solutions,