← All fixes
TypeError: Cannot read properties of null (reading 'addEventListener')
#86Error
TypeError: Cannot read properties of null (reading 'addEventListener')
Why it happens, and why this works
document.querySelector / getElementById found nothing, so it returned null. Either the script runs before the HTML element exists (script tag in <head> without defer), or the id/selector doesn't match the element.
Fix
Load the script after the page is parsed: add defer to the <script> tag (or type="module", which defers by default), or move it to the end of <body>. Then check the selector matches exactly (ids are case-sensitive, '#' for ids, '.' for classes). In React, don't query the DOM; use onClick or a ref.
Did it work?
No reports yet0 worked0 didn't
Tried it yourself?
Related fixes
TypeError: Cannot read properties of undefined (reading 'map')
#61TypeError: Cannot read properties of undefined (reading 'map')
nodejavascriptreactVerifiedNo reports yet
TypeError: Cannot destructure property 'name' of 'undefined'
#60TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.
nodejavascriptVerifiedNo reports yet
Express: req.body is undefined in a POST handler
#37TypeError: Cannot read properties of undefined (reading 'name') at the line that reads req.body.name in an Express POST route