# Installing Packages & The npm Registry

> Master the 5-step lifecycle of npm install, from querying registry packument JSON metadata and tarball fetching to SHA-512 integrity checks and node_modules extraction.

Subject: Package Managers | Lesson 4 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/npm-install-and-registries
Previous: [The "exports" Field & ESM vs CJS](https://codewithanimation.in/package-managers/module-exports-and-entry-points.md) | Next: [Semantic Versioning (SemVer)](https://codewithanimation.in/package-managers/semantic-versioning.md)

## What is npm install & Registry Architecture?

For a registry dependency, the npm CLI obtains package metadata and a compressed archive from the configured registry, using cached content where possible. The public npm registry is the default; scoped registry configuration and other dependency sources can change where packages come from.

Rather than downloading source code directly from Git repositories, npm queries registry endpoints to resolve versions, fetch pre-packaged tarball archives, verify cryptographic integrity checksums, and extract dependencies into the local node_modules directory.

## Why Registry Architecture & SHA-512 Verification Matter

Centralized package registries and structured installation pipelines ensure speed, security, and reproducibility. Packument metadata documents contain version maps, dependency requirements, and distribution tarball URLs without requiring full package downloads during resolution.

Integrity metadata detects a mismatch between an archive and the expected digest. Protect the source of that expected metadata too: a matching digest does not establish trustworthy authorship, harmless behavior or the absence of vulnerabilities.

## The 5 Steps of npm install

1. Manifest Inspection: The CLI inspects package.json to identify required dependency ranges and checks package-lock.json for existing pinned versions.

2. Metadata fetch: for a registry dependency such as lodash, npm obtains the package metadata from the configured registry or an available cache, including version and distribution information.

3. Tree Resolution: npm calculates the dependency tree, selecting exact matching versions according to SemVer rules and hoisting constraints.

4. Download and integrity verification: npm can reuse its cache or fetch the required archive, then check its content against the expected integrity metadata. Matching a digest checks the expected bytes, not whether those bytes contain safe code.

5. Disk Extraction & Lockfile Mutation: The CLI extracts files into `node_modules/` and updates `package-lock.json` with exact version numbers, tarball URLs, and integrity hashes.

## Common mistake: Running npm install in CI pipelines instead of npm ci

npm install normally reuses compatible locked versions, but it can reconcile changed requirements and update the lockfile. In CI, use npm ci when a mismatch must fail rather than being repaired during installation.

npm ci removes the existing node_modules and installs from an agreed manifest/lock without rewriting them. This controls dependency choices; environment, platform and script differences can still affect a build.

## Tip: Configure custom registries and scope overrides in .npmrc

Use .npmrc to map a package scope to its intended registry. For example, an @myorg:registry setting routes that scope; the default registry setting handles packages without a different scope mapping.

Keep authentication scoped to the intended registry host. Supply _authToken through an environment-variable reference such as ${NPM_TOKEN}, rather than committing the actual credential value.

## Try it: Inspect the npm Install Pipeline

Watch how `npm install lodash` queries registry.npmjs.org for packument metadata, verifies the SHA-512 integrity hash, and updates node_modules and package-lock.json.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/npm-install-and-registries

## Check yourself

### Question 1

For a registry package, what do metadata and the tarball provide?

- A. Metadata contains only your local source code
- B. Metadata describes versions; the tarball contains package files
- C. The tarball is your application's lockfile
- D. Both are Git branches

Correct answer: B

The client resolves a package version from metadata and retrieves its archive, unless suitable cached content is already available.

- Why not A: Registry metadata describes published packages.
- Why not C: It contains package files, not the consuming app's lockfile.
- Why not D: Registry documents and archives are not branches.

### Question 2

package.json permits ^1.2.3 and its lockfile resolves 1.4.0. What does a normal npm install use if that lock entry remains valid?

- A. Always the newest published version
- B. Only version 1.2.3
- C. The locked 1.4.0 version
- D. The newest 2.x version

Correct answer: C

A compatible existing lockfile supplies exact resolutions. npm does not automatically discard it just because a newer compatible release exists.

- Why not A: A valid locked version is normally reused.
- Why not B: The range permits other 1.x versions; 1.4.0 satisfies it.
- Why not D: 2.x does not satisfy ^1.2.3.

### Question 3

Must every npm install download every archive from the public npm registry?

- A. Yes, even when all required archives are cached
- B. Yes, private scoped registries are unsupported
- C. No, npm never uses the network
- D. No; registry configuration and cached content can change what is fetched

Correct answer: D

The public registry is the default. Configured registries, other dependency sources and reusable cache content can alter network access.

- Why not A: npm can reuse cached package content.
- Why not B: npm supports registry configuration including scope mappings.
- Why not C: Missing metadata or archives may need network access.

## Remember this

- The npm registry (`registry.npmjs.org`) serves packument JSON metadata and compressed tarball (.tgz) archives.
- npm install executes 5 core steps: Manifest Inspection, Packument Fetch, Tree Resolution, SHA-512 Verification, and Disk Extraction.
- package-lock.json records resolved tarball URLs and SHA-512 integrity hashes for reproducible builds.
- Use `npm ci` in automated CI pipelines for strict lockfile enforcement and fast, clean installs.
