Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 36 ยท Installing and Versions

Installing and Removing Packages

Learn npm install, npm uninstall and npm ci with their Yarn and pnpm equivalents, the flags that matter, what each command changes on disk, and how to read install output.

Installing packages is the command you will type most often. It looks like one action, but there are really three different situations: adding a new package, restoring everything a project already lists, and doing a strict, reproducible install on a server. Each has its own command and its own effect on package.json and the lockfile.

All commands below run on your own computer, so they appear as read-only blocks. The live editors model what each command does to your files.

Adding a package

Terminal
npm install lodash          # alias: npm i lodash
yarn add lodash
pnpm add lodash

This does four things: finds a version in the registry, downloads it with its dependencies into node_modules, adds "lodash": "^4.17.21" (or whichever version is current) to dependencies in package.json, and updates the lockfile. Note the ^ the manager writes in front of the version; the next lessons explain what it means.

Useful flags:

Goal npm Yarn pnpm
Dev dependency -D / --save-dev -D -D
Exact version, no ^ -E / --save-exact -E -E
A specific version npm i [email protected] yarn add [email protected] pnpm add [email protected]
A dist-tag npm i lodash@next same same
Several at once npm i a b c yarn add a b c pnpm add a b c
Node.js What 'npm install -D' changes in package.json

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Restoring everything

Terminal
npm install      # or: npm i
yarn install     # or just: yarn
pnpm install     # or: pnpm i

With no package name, the command reads package.json (and the lockfile) and makes node_modules match. You run this after cloning a repository, switching branches that changed dependencies, or deleting node_modules. If the lockfile already pins every version, the manager uses those versions exactly.

Removing a package

Terminal
npm uninstall lodash     # aliases: remove, rm, un
yarn remove lodash
pnpm remove lodash

This deletes the package from node_modules, removes the entry from package.json and updates the lockfile. Packages that only that package needed disappear too ("pruning"). Deleting the line by hand and running npm install has the same result.

Node.js Uninstall also prunes orphans

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

logger stays as long as anything still needs it; router goes away with web-kit. Package managers do this graph walk for you.

The clean install: npm ci

Terminal
npm ci                              # npm
yarn install --immutable            # Yarn Berry (Classic: --frozen-lockfile)
pnpm install --frozen-lockfile      # pnpm

The "ci" stands for continuous integration, but it is the right command for any build server or Docker image. Unlike npm install it:

  • deletes node_modules first and installs from scratch,
  • never changes package.json or the lockfile,
  • fails if the lockfile and package.json disagree.

That strictness is the point: the build either reproduces exactly what you tested or stops with an error.

Node.js install vs ci: the consistency check

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Reading the output

After npm install, a summary appears: how many packages were added, how many are looking for funding, and the number of vulnerabilities npm audit found. Warnings starting with npm warn are worth reading: deprecated packages, peer dependency conflicts and unsupported engines are common ones. Errors beginning ERESOLVE mean npm could not find versions satisfying every constraint. Useful commands afterwards: npm ls (what is installed), npm ls <name> (who needs it), npm explain <name> (why) and pnpm why <name>.

Never copy-paste sudo npm install. Needing sudo means npm's directories are owned by root, which later breaks permissions. Fix the ownership or use a version manager; see Global Installs and npx.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does npm install (with no package name) do?

    Without arguments it installs everything the project lists.
  2. 2. Which command is right for a CI server?

    npm ci installs exactly what the lockfile records and fails on mismatch.
  3. 3. What does -D (--save-dev) do?

    -D records the package under devDependencies.
  4. 4. What is the Yarn equivalent of npm uninstall lodash?

    Yarn and pnpm both use remove.

Summary

  • npm install pkg / yarn add pkg / pnpm add pkg add a dependency, update package.json and the lockfile.
  • No-argument install restores node_modules; use it after cloning.
  • Uninstall removes the package and prunes anything only it needed.
  • npm ci (or --immutable / --frozen-lockfile) gives strict, reproducible installs for CI and servers.
  • Read warnings; use npm ls and npm explain to understand the tree.

Next: Semantic Versioning