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 (2014) Building a Command Line Application Organizing Your Code with require

alex gwartney
alex gwartney
8,849 Points

require file not working?

Not sure why but the require file is not working I typed every thing in correctly from what i can tell so could some one take a look and see what i might be doing wrong. The error that im getting said unexpected token function.

var profile = require("./profile");
profile.get('chalkers');
var http = require("http"), 

function printerror(error){
  console.error(error.message);
}    
  function printMessage(username, badgeCount, points){
  var message = username + ' has ' + badgeCount + ' total badges and ' + points + ' points in JavaScript.';
  console.log(message);
}

function getprofile(username){
//Connect to the API URL (https://teamtreehouse.com/username.json)
var request = http.get('http://teamtreehouse.com/' + username + '.json', function(response) {
  console.log(response.statusCode);

  var body = "";
    //Read the data
    response.on('data', function (chunk) {
      body += chunk;
    });
   response.on("end", function(){
     if(response.statusCode == 200){
     try{
        var profile = JSON.parse(body)
        printMessage(username,profile.badges.length,profile.points.JavaScript)
     }catch(error){
        printerror(error)
       }
     }else{
      printerror({message:"There was a error for" + username}) 
     }
    });
  });


 request.on('error',printerror);
}    


module.exports.get = getprofile;

2 Answers

Roberto Alicata
PLUS
Roberto Alicata
Courses Plus Student 39,959 Points

In the first line of the profile.js file you wrote a , instead of a ; at the end

var http = require("http");

I suggest to you to use a linter for your editor so you can write a code without syntax errors

alex gwartney
alex gwartney
8,849 Points

yea that was the problem thanks for the help

akak
akak
29,445 Points

You are missing semicolons after printMessage and both printerror in response.on("end"). I'm not sure if that helps but worth a try :)