#ReferenceError: document is not defined

34 messages · Page 1 of 1 (latest)

fiery ravine
#

Hello, I am a complete beginner to js/html/css and I have no idea what I'm doing. I don't really understand what node.js is but I got it because I was having problems running js at all without it. I use vscode.

I'm trying to make a separate file for all my javascript, and am linking it to my html via:
<script src="script.js"></script>

and I am visualizing my html + css with the vscode liverserver extension, however, when I try to run my js file I get ReferenceError: document is not defined

(the code is only trying to grab an html element so far: var originalLine = document.getElementById("line-0");)

I tried googling and stack overflow, but I don't understand what they're talking about in the image I attached.

How do I get my javascript file to recognize the html document and its elements? I think it has to do with nodejs but like I said before, complete beginner I have no clue what anything is or what's happening.
If anyone could help me out I'd seriously appreciate it 🙏

brittle river
#

show your code. if you're using <script> tags, you don't need node

#

node is a JS environment. browsers are another JS environment. each have their own APIs but can both run JS.

coral stone
#

Mandatory #faq message to learn modern web dev

brittle river
#

document is an API provided by browsers. it's not included in node by default.

fiery ravine
brittle river
#

that's not live server, that's node

#

why are you running this in node?

fiery ravine
#

oh what

#

how do I

#

not run it in node

brittle river
fiery ravine
#

sorry I had no idea

coral stone
#

also lots of ew, I would suggest learning modern before continuing with your adventure 😁

fiery ravine
#

lmaoo, I'll look into that, thank you guys

#

wait no sorry I think I'm blind..where is this go live button 😭

#

I've been right clicking and opening

fiery ravine
brittle river
#

you would also open the HTML with live server, not the JS script

fiery ravine
coral stone
#

No, but it's a first step into good practices

fiery ravine
#

the html page is open right now

brittle river
#

that extension is installed?

fiery ravine
brittle river
#

in settings?

fiery ravine
#

yes that's checked in my settings as well

brittle river
#

no clue. restart vscode or something

fiery ravine
#

🫡 will do

brittle river
fiery ravine
#

oh lord okay I got it working now, thank you

#

haha omg I was way overcomplicating this entire thing, I just realized that the js is running automatically- don't need to run it in vscode or node at all