Ask coding questions

← Back to all posts
Require is undefined in javascript
MikeJMS8910 (234)

I am trying to set up express and I downloaded node.js, npm, and all of the things that I would need for it. But when I do const express = require('express') I get the error request is undefined. I have been searching the internet for the past 3 hours but nothing works and I keep getting the same error. How can I fix this?

Answered by Coder100 (18930) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18930)

Did you do it in nodejs or HTML?
Require only works in node.js silly

MikeJMS8910 (234)

@Coder100 yea I did do node.js. I had someone else test it for me and they said that it works for them so I think the problem is from which version of node I have installed on my computer(I am scripting this in vs code not repl.it)

Coder100 (18930)

You set in package.json that the main file is app.js, but you made app.js instead. @MikeJMS8910

MikeJMS8910 (234)

@Coder100 I dont really understand, but then what do I need to do in the package.json

Coder100 (18930)

change "main": "index.json" to "main": "app.json" also you still have to sent screenshot of the error. @MikeJMS8910

Coder100 (18930)

oh i see
you have to actually run it with node node app.js @MikeJMS8910

Coder100 (18930)

so like hit win + r, type in cmd.exe, press enter and then cd to your project folder and do node . @MikeJMS8910

MikeJMS8910 (234)

@Coder100 ok I know what I was doing wrong now. I wasnt running the script like you said and then I was going to the file directory instead of localhost:3000