Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Node.js Basics 2017 Handling Errors in Node Handling the Error Event in Node

Uncaught ReferenceError: require is not defined

g the I tested the code in workspaces as well as my browser. Everything seems to be aligned with what Andrew created but I keep getting require is not defined but i see it clearly defined and has a value Here is my code:

// Problem: We need a simple way to look at a user's badge count and JavaScript points

const https = require('https'); // Solution: Use Node.js to connect to Treehouse's API to get profile information to print out function printMessage(username, badgecount, points) { const message = ${username} has ${badgecount} total badge(s) and ${points} points in JavaScript; console.log(message) }

function getProfile(username) { // Connect to the API URL (https://teamtreehouse.com/username.json) const request = https.get(https://teamtreehouse.com/jasminetaylor2.json, response => { let body = ""; // Read the data response.on("data", data => { body += data.toString(); }); response.on("end", () => { // Parse the data const profile = JSON.parse(body); // Print the data
printMessage(username, profile.badges.length, profile.points.Javascript)

    });


});
request.on('error', error => console.log(`Problem with request: ${error.message}`))

} const users = process.argv.slice(2) users.forEach(getProfile)

Bryon Czaja
Bryon Czaja
5,922 Points

Hello Jasmine, It looks like you have extra braces and parenthesis. Though, the formatting is looking sort of weird from where I am sitting...

const https = require('https'); // Solution: Use Node.js to connect to Treehouse's API to get profile information to print out function printMessage(username, badgecount, points) { const message = ${username} has ${badgecount} total badge(s) and ${points} points in JavaScript; console.log(message) }

function getProfile(username) { 
    // Connect to the API URL (https://teamtreehouse.com/username.json) const request = https.get(https://teamtreehouse.com/jasminetaylor2.json, response => { let body = ""; // Read the data response.on("data", data => { body += data.toString(); }); response.on("end", () => { // Parse the data const profile = JSON.parse(body); // Print the data
    printMessage(username, profile.badges.length, profile.points.Javascript)
}


request.on('error', error => console.log(`Problem with request: ${error.message}`));
const users = process.argv.slice(2) users.forEach(getProfile);
Bader Alsabah
Bader Alsabah
4,738 Points

Hi Jasmine,

The code will NOT work within the browser and that's why you're getting "require is not defined error". Node is an implementation of JavaScript outside of the browser or "document-object-model". So the browser is unaware of handling such situations. The two options are to use workspaces as it creates a programming environment with Node for you, or to download Node on your machine and a use a text editor like Visual Studio Code or Atom. I recommend the latter option as it sets you up for real life projects and gets you comfortable with a code editor from the start.

Hope this helps :)

1 Answer

Yuri Souza
Yuri Souza
9,518 Points

Hi Jasmine,

as i cannot see your whole code i'm just going to have a wild guess and say your error handler (request.on....) is outside the function getProfile(). What is happening here is your ''require'' variable isn't being recognized because it's outside the variable scope. Hopefully this will help anyone having this issue too.

And by the way, don't try to run this code on a browser, it won't work!