All courses

Dependencies, package managers and lockfiles

Explain that projects pull in external packages through a package manager (pip, npm, ...), listed in a manifest and pinned in a lockfile. Explain why installing a package runs someone else's code on your machine.

Builds on: How a code project is laid out

Read it card by card Start the whole course

You asked for 1 package and got 69

You run `npm install express`. npm replies `added 69 packages`. Next to `package.json`, a file you never wrote appears: `package-lock.json`, hundreds of lines long. Why 69? And next month, when a teammate installs the project, which file decides the versions they get: `package.json`, or that new file? If you use pip: does `requirements.txt` do the same job?

Borrowed code, written down twice

Most projects reuse code that others published, called **packages**. A **package manager** (`npm` for JavaScript, `pip` for Python) downloads them from a public **registry**. The **manifest** (`package.json`) lists the packages you asked for, each with a range of allowed versions. The **lockfile** (`package-lock.json`) records the exact version of every package installed, including the packages those packages need. The package files themselves go into `node_modules/`. That folder is normally left out when the project is shared, because installing rebuilds it.

Where to look, what to touch

- **JavaScript:** `npm` · manifest `package.json` · lockfile `package-lock.json` · files in `node_modules/` - **Python:** `pip` · manifest `requirements.txt` or `pyproject.toml` · install the listed packages with `pip install -r requirements.txt` · `pip install` writes no lockfile, but tools like Poetry and uv do (`poetry.lock`, `uv.lock`) · files often in `.venv/` Rules: 1. Add packages with the manager's command (`npm install <name>`). Never write the lockfile by hand. 2. To get exactly the same versions, install from the lockfile (`npm ci`). 3. Don't copy install folders around, because they contain files set up for one specific machine. Reinstall instead.

One install, many packages

You asked for one package and got 69, because `express` needs others, and those need others too. The manifest stores a range: `^4.21.2` means "4.21.2 or any newer 4.x". The lockfile pins the exact `4.21.2`. `-A1` makes `grep` also print the line after each match. A `\` at the end of a line continues the command, so `package-lock.json` is part of the command, not output. Counts and versions are illustrative.
$ npm install express
added 69 packages in 3s
$ grep '"express"' package.json
    "express": "^4.21.2"
$ grep -A1 '"node_modules/express"' \
    package-lock.json
    "node_modules/express": {
      "version": "4.21.2",

Which file your teammate gets

A month later, a newer 4.x of `express` is out, and some of the other 68 packages have new versions too. Your teammate runs `npm ci` and gets exactly what the lockfile says: `express` 4.21.2 and the same version of every other package. Without the lockfile, npm would pick the newest versions each range allows, so their copy could quietly differ from yours. So share `package-lock.json` with the project. Python works the same way: `pyproject.toml` asks for `requests>=2.31` (2.31 or anything newer), and `uv.lock` pins `2.32.3`. A `requirements.txt` with ranges like that is only a manifest. It pins nothing.

Remember

A package manager fetches the packages you ask for, plus every package they need. The manifest (`package.json`, `pyproject.toml`) says what you asked for, as version ranges. The lockfile (`package-lock.json`, `uv.lock`) pins exactly what you got, down to every nested package, and it decides what your teammate gets. Add packages with the manager's command, never edit the lockfile by hand, and share it with the project.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.