NPM

NPM, Node Package Manager, is a tool for finding and installing Node modules. NPM has two major parts:

  1. A registry of modules.
  2. Command line tools to install modules.

NPM Registry

The NPM registry is a listing of thousands of modules that are stored on a remote server. These can be required and downloaded to your project. The modules have been contributed by other developers just like you.

There is an online version of the registry where you can search for a module by name or desired functionality.

Example

Go to online NPM registry and enter “readline-sync” into the search packages input box.

Results for searching readline-sync within the NPM registry Results for searching readline-sync within the NPM registry

An exact match appears as the first result. That is the readline-sync module we required. Clicking on the first result leads to the NPM page that describes the readline-sync module.

On the details page you will see:

  1. Usage statistics (how often the module is used)
  2. Instructions on how to use the module (example code)
  3. Version information
  4. The author(s)
  5. Sourcecode repository

readline-sync npm registry homepage readline-sync npm registry homepage

NPM Command Line Interface (CLI)

The NPM command line tool, CLI, is installed with Node. The NPM CLI is used in a computer’s terminal to install modules into a Node project.

For now, recall that we coded many Node projects inside of Visual Studio Code.

Example

Even though we added the readline-sync dependency to existing package.json files, our code still required additional configuration because input (or whatever you may decide to name your variable) is not defined automatically. The final step of requiring readline-sync is to assign it to a variable in addition to the require command. You may have noticed the following line or something very similar in previous chapters within your starter code for exercises, studios, or assignments.

const input = require("readline-sync");

Example Use Case

const input = require("readline-sync");

const name = input.question("What is your name?");
console.log(`hello ${name}`);

Once dependencies are installed into a project there may need to be additional configuration done on the user’s behalf within their project.