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 Building a Command Line Application Making a GET Request with https

Petar Pav
Petar Pav
4,728 Points

const https = require('https'); - getting error in chrome

Hi guys, I am getting error on ' require ' when trying to print the request in the browser. Any ideas why does it happens? Probably is something right in front of my noose but cant see my own mistake. Here is the code:

//Solution: Use Node.js to connect to Treehouse's API to get profile information to print out 

//Require https module
const https = require('https');

const username = "petarpav";

//Function to print to html 
function print (message) {
  const outputDiv = document.getElementById('textfield');
  outputDiv.innerHTML = message; 
}
//Function to print message to console 
function printMessage(username, badgeCount, points) {
const message = `${username} has ${badgeCount} total badges and ${points} points in JavaScriptd`;  
  print(message);
 }

printMessage("Petar", 200, 10000);


// Connect to the API URL (https://teamtreehouse.com/username.json)
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {

        print(response);
    //Read the data
    //Parse the data 
    //Print the data

});

The error in browser is " Uncaught ReferenceError: require is not defined at app.js:6"

1 Answer

Steven Parker
Steven Parker
231,236 Points

This is not browser code. This is code to be run on a server or workstation using node.js.

The course provides a workspace where you can run node (and node scripts) without installing it on your own machine.

Petar Pav
Petar Pav
4,728 Points

Thanks Steven, noticed few minutes after posted it, also I should remember about host and native objects.