[SOLVED] Document is not defined

Hello all :slight_smile:
I’been trying to follow the tutorial to build a simple dex by using the Moralis sdk. I’m not sure I’m doing the rights steps, as I don’t have visual studio but webstorm from intellij.

So here is the thing, webstorm allows me to open the index.html via chrome, or local built-in webpage, and it works, however it seems it’s not able to fetch the list of tokens, because when I click on the swap selection window the list is just empty. So I thought the issue is that the main.js may have some issues after it’s called by index.html. If I run the main.js singularly, I’d expect it to raise no errors, however it does as the following:
ReferenceError: document is not defined.

I see from the code that indeed the document function/object is not defined/declared in the script. I got the script from the full code reference of the youtube tutorial, but I don’t know if I missed some steps or anything.

Thx a lot for the help in advance,pls let me know if I should paste some snippet of the code here.

Hey @filippodev86

I suggest you download VS code and install “Live Server” extension to be able your front end locally

I tried, but got the same result. I did some digging, and it seems node js doesn’t support the document framework because is server-side. Live server of VS yet raises the same error, weird. Is this built-in document function related to the domain the website is supposed to work on? sorry for newbie question :slight_smile:

What is the line that gives an error in the browser console?

The end of the script is the following snippet:


document.getElementById("modal_close").onclick = closeModal;

document.getElementById("from_token_select").onclick = () => {
document.getElementById("to_token_select").onclick = () => {
document.getElementById("login_button").onclick = login;
document.getElementById("from_amount").onblur = getQuote;
document.getElementById("swap_button").onclick = trySwap;

The error is raised as soon as the first document function is called:

document.getElementById("modal_close").onclick = closeModal;

and you are running this in a browser, right (with a local http server)?

I mean usually document.getElementById works fine in a browser in javascript

Not sure if it’s what you mean, but when I select to “run” the html file a browser page opens automatically(local host), however I suspect the main.js file is simply run with nodejs which may be the cause why the document function is not defined. Let’s say I’d want to run everything directly on a browser, how can I do that?

usually you open the html in the browser, and the js file is loaded by the html file and everything runs in the browser

Ok, it could be something with my settings so that the main.js is run by my editor on nodejs for some reasons. I’ll try to check that and in case report here :slight_smile:


I solved :smiley:

there was a feature in the editor to run the index.html specifically on browser instead of just “run” (which instead was wrongly using nodejs for main.js).

super duper! :wink:

I didn’t know about this feature …