0

I'm trying for the first time NodeJs, I installed NodeJs, npm and all needed modules for my project. Now I want to run it on my website (still on localhost). But when I open console there is error saying Uncaught ReferenceError: require is not defined at HTMLDocument. And then specification on which line in code I used require. It is simple line const bodyParser = require("body-parser"); but on the website it does not work. Is there something more I need to do?

Beginner
  • 11
  • 2
  • How do you execute your javascript code after you're done writing it? – JayCodist Aug 16 '20 at 10:50
  • 2
    Does this answer your question? [Client on node: Uncaught ReferenceError: require is not defined](https://stackoverflow.com/questions/19059580/client-on-node-uncaught-referenceerror-require-is-not-defined) – Simperfy Aug 16 '20 at 10:51
  • I don't. I thought that website would take care of it, just like with other javascript code. – Beginner Aug 16 '20 at 11:18

2 Answers2

0

require is only used from server-side and not usable on browser. And please know that NodeJs is JavaScript RunTime for server-side programming.

critrange
  • 5,652
  • 2
  • 16
  • 47
0

yash is right. If you want to import a module on front-end side, you can use ES6's import. All modern browsers support it. But if you want to import in oldest browsers too, read about browserify where you can use special require function.

hazer_hazer
  • 136
  • 1
  • 7