{ Introduction to NPM. }

Objectives:

By the end of this chapter, you should be able to:

  • Define what NPM is
  • Install packages using NPM
  • Explain how a package.json file works
  • Explain why we add the node_modules folder to a .gitignore file

NPM introduction

When you install node, you also get access to a package manager that you can use to install external node modules, or modules that do not come built in with Node.js. This package manager is called npm and we will be using it to install packages. To manage these packages, npm uses a special file called a package.json that stores information about the packages we have installed.

Note - there is another package manager called yarn, which is gaining popularity, but we will be using npm for this tutorial. You can read more about yarn here.

package.json

To create a package.json file we can run the command npm init. This will begin the creation process. As part of the process you will be prompted for a fair amount of information about your project; to accept the default answers provided by npm, just keep hitting enter. We will discuss in more detail what these options are, but for now you can simply keep pressing enter until the file is created. To create a package.json without having to keep pressing enter you can simply run npm init --yes or npm init -y

Installing external modules

To install an external module we use the npm install NAME_OF_PACKAGE command. It's essential that when installing modules, we add the name of the module and version number to our package.json file. This will help other developers install and use the same packages and help us remember which packages we installed. We could manually type these into our package.json, but thankfully there is an easier way. When installing a package make sure to add the --save flag after.

Let's install an external module called request. This module is very useful for making server side requests (to other APIs). To do this we will first run the following commands to create a folder, package.json and essential files. This will all be run in terminal.

mkdir external_modules
cd external_modules
npm init --y # create a package.json without pressing enter over and over
npm install --save request # install the request module
touch main.js

Inside of our main.js, let's add the following.

var request = require("request");

request('http://swapi.co/api/people/1', function(error, response, body) {
    if (!error && response.statusCode == 200) {
        console.log(JSON.parse(body)); // Show the JSON for the Star Wars Character
    }
});

Now if we run node main.js we should see some JSON data about Luke Skywalker!

Ignoring node_modules

When we install the request module, a folder gets created in the root directory of our application called node_modules. If you look inside of here, you will actually see every single module that the request module depends on (we call these 'dependencies'). Since this file is quite large, when we push our code on GitHub, we do not want the node_modules folder to be there. To avoid git tracking this folder, we add the text 'node_modules' to a .gitignore file. The terminal command to do this is echo node_modules > .gitignore.

You may be wondering, what happens if someone forks and clones our repository? How do they know which packages to install from npm? This is exactly why it is so important to add a --save every time that you npm install. If a package.json contains the dependencies for a project, you can simply run npm install and npm will search through the package.json file for all dependencies and install them.

When you're ready, move on to Node.js Exercises

Continue