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 Capturing Command Line Arguments

Anna Shalaginova
Anna Shalaginova
3,160 Points

Print part of the program does not work when I add process.argv

Here is the code:

const https = require('https')

function printMessage(username, badgeCount, points){
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript.`;
  console.log(message);
}
function getProfile(username){
const request = https.get(`https://teamtreehouse.com/${username}.json`,
  response => {
    let body = "";
    response.on('data', data => {
      body += data.toString();
    });

    response.on('end', () => {
      // Parse the data
      const profile = JSON.parse(body);
      // Print data
      printMessage(username, profile.badges.length,
      profile.points.JavaScript);
    })

  });
}
const users = process.argv.slice(2);
// const users = ["chalkers", "alenaholligan", "davemcfarland"];
users.forEach(getProfile);

When I run 'node app.js' in the terminal, this is the output that I get:

[ '/Users/annashalaginova/.nvm/versions/node/v10.15.0/bin/node', '/Users/annashalaginova/sei/studies/node/app.js' ]

However, I am suppose to see this:

alenaholligan has 135 total badge(s) and 205 points in Javascript. davemcfarland has 304 total badge(s) and 8353 points in Javascript. chalkers has 209 total badge(s) and 5987 points in Javascript.

4 Answers

You need to run node with the command line arguments

node app.js chalkers alenaholligan davemcfarland   

Alejandro, the teacher's notes for the video have a link to process.argv documentation.

The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. The first element will be process.execPath. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command line arguments.

See the example on that page. For the code on this page

const users = process.argv.slice(2);

is the arguments passed in, the 'remaining elements of additional command line arguments' at index 2 - in this case the names chalkers alenaholligan davemcfarland.

If you need additional help it might be better (faster) to create a new topic. Then it will be seen by the whole community.

Thank you so much, it took some time to understand yesterday. I got the concept. I was missing the fact that he passed in the names when newly calling the node appp.js command. I thank you for the help though!

I don't understand this... How come the names in the array can be used if the array is commented out? Also, what does the process.argv.slice(2) declaration have to do in this whole process? I'm super confused as to how the usernames could be used when they're technically not even in the code? Can someone please clarify?