liveBook · Manning

Npm

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

The default package manager for Node.js that installs packages and manages project metadata and dependencies.

npm is the command-line package manager installed with Node.js. It connects a project to the online npm repository, where you can find libraries and tools instead of writing every feature yourself. For a Node.js microservice, it creates project metadata and installs dependencies such as Express. section 2.3 section 2.5.4 section 2.6.2

Start in a terminal:

  • Create a project directory, change into it, and run `npm init -y`. The `-y` option accepts defaults instead of asking interactive questions; npm creates `package.json`. section 2.6.1
  • Run `npm install express` to add Express. npm installs the package under `node_modules` and records it as a dependency in `package.json`. section 2.6.2
  • When working from an existing project, run `npm install` in its directory to install its dependencies. section 2.6.2

`package.json` holds project metadata, scripts, and top-level dependencies. npm generates and updates it, but you can edit it by hand. Dependency entries can specify a version or a range; dependencies of dependencies are handled automatically. The older `--save` option makes dependency tracking explicit, but it is no longer required. section 2.6.1 section 2.6.2

Listing 2.2 The package file with Express installed
{
  "name": "example-1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
   --snip--
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^5.0.0-beta.1"      #1
  }
}  

Inspect the `dependencies` entry to see how an installed Express version is recorded in `package.json`. section 2.6.2

Check `node --version` and `npm --version` after installing Node.js. For production work, keep the Node.js version used in development aligned with production. section 2.5.4

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works through initialization, installation, and dependency declarations; does not explain npm internals.
Examples
Uses `npm init -y`, `npm install express`, and an Express dependency entry in `package.json`.
What it assumes
Assumes terminal access and a Node.js installation, while explaining npm project setup from an empty directory.
What you can do after
Apply it via npm to initialize a project, install Express, and inspect its recorded dependency.
How it connects
Connects Node.js projects, package.json, node_modules, dependency versions, and Express.

Also explained in

Compare how 5 books explain npm →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.