Skip to content
← Back to Community
Wierd Nodejs Behaviour
Profile icon
SamKunefeh

Hello Everyone!

So in this repl I made a simple express server and made it serve index.html. The html file has a script tag for running script.js and it executes perfectly because a new element is added to the web page. However if I uncomment line 7 or lines 13-18 of script.js, the element is not added. Why does that happen? I am guessing it has something to do with nodejs vs browser javascript, but I still can’t wrap my head around what’s happening.

Answered by Coder100 [earned 5 cycles]
View Answer
Voters
Profile icon
SamKunefeh
Comments
hotnewtop
Profile icon
Coder100

anyways, script.js isn't nodejs -- its web js!! So that means you can't use axios or any of the cool node things. It's not node!

Profile icon
SamKunefeh

@Coder100
Wait, so is there any way I convert it to nodejs? Or would I just have to use it the web/dom way? And if that’s the case, why doesn’t the code work with the event listener eventhough thats also in webjs?

Profile icon
Coder100

you have to use it the dom way
you don't convert anything to nodejs -- its already js!
nodejs is just js but backend.

@Jackbaklava

Profile icon
SamKunefeh

@Coder100
Oh ok, that makes sense. So I’ll probably have to use script tags with the commonjs import syntax instead of require. But can I still use npm packages?

Profile icon
SamKunefeh

@Coder100
Nevermind, I think I’ll have to use browserify or unkpg. Thanks for your help!

Profile icon
Coder100

np!
btw
some node packages won't work because of what they try to accomplish, for example puppeteer.
anyways mark as correct answer?

@Jackbaklava

Profile icon
Coder100

that's not nodejs anymore -- that would be just dom js