ReferenceError: require is not defined


#1

Hi all. Real beginner here. I’m trying to emulate this simple cheerio tutorial in glitch: https://www.youtube.com/watch?v=LJHpm0J688Y
However it keeps throwing back ReferenceError: require is not defined

Here is my project https://cheeriotest.glitch.me/

I think this is because I don’t fully understand how glitch is structured. Should I be var requiring my libraries some other way?

Thanks


#2

The issue is that the tutorial is being run in Node, and you’re trying to put your scraping code into client.js, which runs in the browser. require() is built into Node, which is how we run your server-side code in Glitch. You should be able to get this to work by moving your code to server.js. Then your scraping code will run, and you can use console.log() to print messages that will appear in the Glitch logs.

You could just add your scraping code at the bottom of server.js, and then the scraping will happen every time the server is restarted. This is the easiest thing to do, and probably where you should start. Any time you change the code in server.js, the server is restarted, and your code will run again.

The other way to go would be to add your code to a route handling function, and then you would be able to cause the scraping to happen by hitting the server from a browser, and you could add some code to return the results to the browser.


#3

Thank you! I dumped the code at the bottom of server.js and that didn’t seem to work, but I’m not getting a require not found error. Anyway I didn’t realise I should be putting the node stuff in server.js so that’s very helpful, I should probably do some homework on how node works before I dive into cheerio. Thanks!!!