Node.js Require() Beginning require() use with an NPM package


Example

Node's require is also very helpful when used in tandem with an NPM package. Say, for example, you would like to use the NPM package require in a file named getWeather.js. After NPM installing your package through your command line (git install request), you are ready to use it. Your getWeather.js file might like look this,

var https = require('request');

//Construct your url variable...
https.get(url, function(error, response, body) {
  if (error) {
    console.log(error);
  } else {
    console.log('Response => ' + response);
    console.log('Body => ' + body);
  }
});

When this file is run, it first require's (imports) the package you just installed called request. Inside of the request file, there are many functions you now have access to, one of which is called get. In the next couple lines, the function is used in order to make an HTTP GET request.