{ 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 current project, including its package dependencies.

Here is npm's website. You can browse all the available node packages or even sign up to be able to create your own npm packages.

Initializing a Node Project

To initialize a Node project and create a package.json file we can run the command npm init. This will begin the creation process and prompt you for a bunch of information about your project, which you can simply keep pressing "enter" to use default values. Or, to create a package.json without having to keep pressing enter, you can simply run npm init --yes or npm init -y.

Note that these prompts serve two purposes:

  1. To initialize an npm project in case you want to make your own package
  2. To generate a package.json for dependency and script listing.

There is no problem with using a package.json only for #2, as that is what most projects use it for.

Installing external modules

To install an external module we use the npm install command. The npm install command will by default look for a local package.json file to install packages from its list of dependencies. Or, if you provide a package name, e.g. npm install express, it will install the package and save it to a local package.json.

Prior to npm version 5, when installing modules, you had to type npm install express --save to explicitly save the package to the package.json file. Now, luckily, --save is by default. Sometimes you can add the --save-dev flag to install dependencies that are only required in a non-production environment (more on that later).

Let's install an external module called request. This module is 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 request # install the request module
touch main.js

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

const 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 folder can get huge and we already have our list of dependencies in package.json, we don't want to commit and push this folder to GitHub. To avoid git tracking this folder, we add the text 'node_modules' to a .gitignore file. The shell command to do this is echo node_modules > .gitignore.

Global Installs

There are some npm packages that function more like command-line tools since they get run from the terminal. These packages are installed using a -g or --global flag and DO NOT save to a local package.json. Instead, they are saved in a node_modules folder at the root of your Node installation. Be very careful with what you choose to install globally, because you might forget to add dependencies to your project because you simply have them installed globally on your machine. Most projects should be self-contained with their package.json listing all dependencies. For more information, check out this article.

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

Continue

Creative Commons License