Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 36 ยท Getting Started

What Is a Package Manager?

Learn what a package manager does, why JavaScript projects depend on hundreds of packages, and how npm, Yarn and pnpm fit together with the registry and node_modules.

Almost every JavaScript project is built from other people's code. A web server needs a router, a front end needs a build tool, a test suite needs a test runner. Copying those files into your project by hand would be slow, error-prone and impossible to update. A package manager automates that work: it finds the code you asked for, downloads it, puts it where your program can load it, and remembers exactly which versions you are using.

In this course you will learn three package managers: npm, Yarn and pnpm. They solve the same problem with the same ideas, so learning one teaches you most of the other two. This first lesson explains the vocabulary and the moving parts, with small runnable models you can experiment with.

What runs on this site. The runners here can execute Node.js programs but have no network and no npm. So the commands npm install, yarn add and pnpm add are shown as read-only code blocks that you run on your own computer. The live editors use plain Node.js to model what a package manager does (reading package.json, comparing versions, walking folders). Every stand-in is labelled.

The problem a package manager solves

Imagine you write a small program that formats dates. You find a library called date-fns, download its files, and drop them into a lib folder. Three months later the library fixes a bug. You have to remember where you got it, find the new version, replace the files, and check nothing else broke. Now multiply that by 300 libraries, each of which needs its own libraries.

A package manager takes over these jobs:

  • Finding a package by name in a registry (an online catalog).
  • Resolving which exact version to use, including the packages that package needs (its dependencies).
  • Downloading and unpacking the files into your project.
  • Recording the result so a teammate or a server gets the same files later.
  • Running project tasks such as build and test through scripts.
  • Publishing your own packages so others can use them.

The four moving parts

The four moving parts

Package A folder of code with a package.json
Registry An online catalog of packages
Package manager The tool you run (npm, Yarn, pnpm)
node_modules Where installed code lands

Every command you learn touches one of these four.

1 / 2

A package is a folder described by a file named package.json. It might be a library you install (like a date formatter), a command line tool, or your own application. A registry is a web service that stores published packages and serves them by name and version. The default registry for all three tools is https://registry.npmjs.org, run by npm, Inc. (part of GitHub). The package manager is the program you run in a terminal. And node_modules is the folder inside your project where installed packages end up, so that import and require can find them.

npm, Yarn and pnpm in one paragraph each

npm (Node Package Manager) is the original. It ships with Node.js, so if you have Node you already have npm. It is the default everywhere and the one every tutorial assumes.

Yarn was created at Facebook in 2016 to make installs faster and more reproducible at a time when npm was slow and had no lockfile. It exists in two generations: Yarn Classic (1.x) and the modern Yarn (2 and later, often called Berry), which can run without a node_modules folder at all.

pnpm (performant npm) stores every package version once in a global content-addressable store and links it into projects, which saves disk space and time. It is also strict by default: a package can only import what it declared.

All three read the same package.json, all three talk to the same registry, and a package published with any of them can be installed with any other.

A mental model you can run

The simplest version of a package manager reads a list of wanted packages and copies each one into node_modules. The following program models that using an in-memory "registry" and a temporary folder. It is a stand-in: real tools download tarballs over HTTPS, but the shape of the work is the same.

Node.js A toy package manager (stand-in)

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.

Real package managers add the hard parts: choosing versions that satisfy ranges, installing dependencies of dependencies, caching downloads and verifying checksums. You will meet each of those in the coming lessons.

Direct and transitive dependencies

The packages you list yourself are your direct dependencies. Each of those has its own package.json with its own dependencies, and so on. The ones you did not ask for but receive anyway are transitive dependencies. A project with ten direct dependencies often ends up with several hundred packages in total.

Node.js Counting a dependency tree

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.

Notice that logger is needed by two packages but appears once. Deciding when to share one copy and when to keep two (because they need different versions) is a big part of what distinguishes npm, Yarn and pnpm, as you will see in How node_modules and Resolution Work.

The commands you will use most

On your own computer, with Node.js installed, the everyday workflow looks like this. These are read-only examples:

Terminal
npm init -y          # create a package.json
npm install lodash   # add a dependency and install it
npm run build        # run the "build" script from package.json
npm test             # run the "test" script

Yarn and pnpm have near-identical equivalents (yarn add lodash, pnpm add lodash). A comparison table appears in npm, Yarn and pnpm Compared.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a registry?

    The registry (by default registry.npmjs.org) is the online catalog. node_modules is the local folder; package.json describes a project.
  2. 2. Which of these is a transitive dependency?

    Transitive dependencies arrive because something you depend on depends on them.
  3. 3. Which statement about npm, Yarn and pnpm is true?

    All three share the package.json format and the registry, so a package works with any of them.
  4. 4. Why does this site model package managers with plain Node.js programs?

    The runners are sandboxed without network access, so installs cannot be run live.

Summary

  • A package manager finds, downloads, records and runs code so you do not copy files by hand.
  • The four parts: package (with package.json), registry, package manager, node_modules.
  • npm ships with Node.js; Yarn and pnpm are alternatives that share the same file format and registry.
  • Direct dependencies are the ones you list; transitive ones come from your dependencies' own lists.
  • An install reads package.json, resolves versions, fetches code and records the result.

Next: Node.js, npm and Version Managers