Shell scripting with Node.js
You can buy the offline version of this book (HTML, PDF, EPUB, MOBI) and support the free online version.
(Ad, please don’t block.)

6 An overview of npm (a package manager for JavaScript)



6.1 The npm package manager

The npm registry is the de-facto standard for hosting JavaScript packages. Those packages have a particular format and are called npm packages.

Therefore, in the JavaScript ecosystem, a package manager is a command line tool for installing npm packages – from the npm registry or other sources.

The most popular package manager is called npm and comes bundled with Node.js. Its name originally stood for “Node Package Manager”. Later, when npm and the npm registry were used not just for Node.js packages, the definition was changed to “npm is not a package manager” (source).

There are other popular package managers such as yarn and pnpm. All of these package managers use the npm registry by default.

We use npm via the shell command npm which provides several subcommands such as npm install.

6.2 Getting help for npm

6.2.1 Getting help on the command line

We can use the npm command to explain itself: On one hand, there is the option -h which can be used after npm and after npm commands. It provides brief explanations:

npm -h        # brief explanation of `npm`
npm <cmd> -h  # brief explanation of `npm <cmd>`

On the other hand, there is the command npm help which provides longer explanations:

npm help         # brief explanation of `npm` (same as `npm -h`)
npm help npm     # longer explanation of `npm`
npm help <cmd>   # longer explanation of `npm <cmd>`
npm help <topic> # longer explanation of <topic>

Help topics include:

6.2.2 Getting help online

The official npm documentation is also available online.

6.3 Common npm commands

These are a few common commands:

The npm documentation has a list of all npm commands.

6.4 Abbreviations for npm commands

Many npm commands have abbreviations – for example:

Short Long
npm i npm install
npm rm npm uninstall
npm run npm run-script

For each npm command it describes, the npm documentation also lists all of its aliases (including abbreviations).