Node.js package management system: Using npm

As described in Chapter 2, Setting up Node.js, npm is a package management and distribution system for Node.js. It has become the de facto standard for distributing modules (packages) for use with Node.js. Conceptually, it’s similar to tools such as apt-get (Debian), rpm/yum (Red Hat/Fedora), MacPorts/Homebrew (macOS), CPAN (Perl), or PEAR (PHP). Its purpose is to publish and distributing Node.js packages over the internet using a simple command-line interface. In recent years, it has also become widely used for distributing front-end libraries like jQuery and Bootstrap that are not Node.js modules. With npm, you can quickly find packages to serve specific purposes, download them, install them, and manage packages you’ve already installed.

The npm application extends on the package format for Node.js, which in turn is largely based on the CommonJS package specification. It uses the same package.json file that’s supported natively by Node.js, but with additional fields for additional functionality.

1. The npm package format

An npm package is a directory structure with a package.json file describing the package. This is exactly what was referred to earlier as a directory module, except that npm recognizes many more package.json tags than Node.js does. The starting point for npm’s package.json file is the CommonJS Packages/1.0 specification. The documentation for the npm package.json implementation is accessed using the following command:

$ npm help package.json

A basic package.json file is as follows:

{ “name”: “packageName”,

“version”: “1.0”,

“main”: “mainModuleName”.

“bin”: “./path/to/program”

}

Npm recognizes many more fields than this, and we’ll go over some of them in the coming sections. The file is in JSON format, which, as a JavaScript programmer, you should be familiar with.

There is a lot to cover concerning the npm package.json format, and we’ll do so over the following sections.

2. Accessing npm helpful documentation

The main npm command has a long list of subcommands for specific package management operations. These cover every aspect of the life cycle of publishing packages (as a package author), and downloading, using, or removing packages (as an npm consumer).

You can view the list of these commands just by typing npm (with no arguments). If you see one you want to learn more about, view the help information:

$ npm help <command>

 The help text will be shown on your screen.

Before we can look for and install Node.js packages, we must have a project directory initialized.

3. Initializing a Node.js package or project with npm init

The npm tool makes it easy to initialize a Node.js project directory. Such a directory contains at the minimum a package.json file and one or more Node.js JavaScript files.

All Node.js project directories are therefore modules, going by the definition we learned earlier. However, in many cases, a Node.js project is not meant to export any functionality but instead is an application. Such a project will likely require other Node.js packages, and those packages will be declared in the package.json file so that they’re easy to install using npm. The other common use case of a Node.js project is a package of functionality meant to be used by other Node.js packages or applications. These also consist of a package.json file plus one or more Node.js JavaScript files, but in this case, they’re Node.js modules that export functions and can be loaded using require, import(), or import.

What this means is the key to initializing a Node.js project directory is creating the package.json file.

While the package.json file can be created by hand – it’s just a JSON file after all – the npm tool provides a convenient method:

$ mkdir example-package

$ cd example-package/

$ npm init

This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible defaults. 

See `npm help json` for definitive documentation on these fields and exactly what they do. 

Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file. 

Press ^C at any time to quit.

package name: (example-package)

version: (1.0.0)

description: This is an example of initializing a Node.js project

entry point: (index.js)

test command: mocha

git repository:

keywords: example, package

author: David Herron <david@davidherron.com>

license: (ISC)

About to write to /home/david/example-package/package.json: 

{

“name”: “example-package”, “version”: “1.0.0”,

“description”: “This is an example of initializing a Node.js project”,

“main”: “index.js”, “scripts”: {

“test”: “mocha”

},

“keywords”: [

“example”,

“package”

],

“author”: “David Herron <david@davidherron.com>“,

“license”: “ISC”

} 

Is this OK? (yes) yes 

In a blank directory, run npm init, answer the questions, and as quick as that you have the starting point for a Node.js project.

This is, of course, a starting point, and as you write the code for your project it will often be necessary to use other packages.

Source: Herron David (2020), Node.js Web Development: Server-side web development made easy with Node 14 using practical examples, Packt Publishing.

Leave a Reply

Your email address will not be published. Required fields are marked *