JavaScript Node.js Basics 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
171,373 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.