[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:

init();

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

document.getElementById("from_token_select").onclick = () => {
    openModal("from");
};
document.getElementById("to_token_select").onclick = () => {
    openModal("to");
};
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:

Thanks!!

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 ā€¦