npm & Package Management

How Node.js projects declare, install and lock their dependencies with npm and package.json.

What is it?

Almost no backend is written from scratch. A project uses a web framework, a database driver, a validation library, and dozens of packages those depend on. npm (Node Package Manager) installs and manages all of it.

Every Node project has a package.json file that lists:

  • Its name.
  • dependencies: code needed to run in production, like a web framework.
  • devDependencies: tools only needed while developing, like a test runner.
  • scripts: shortcuts like npm run dev.

npm install reads that file, downloads everything listed (plus what those packages depend on), and puts it in a node_modules folder.

Explain like I'm 10

package.json is your project's shopping list: 'we need this framework, this validator, and this test tool only while building.' node_modules is the stocked pantry you get from that list. The lock file is the receipt, recording the exact brand and size of everything, so the next trip buys exactly the same items.

Examples

A minimal package.json

{
  "name": "my-api",
  "version": "1.0.0",
  "scripts": {
    "dev": "node server.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.19.0",
    "pg": "^8.11.0"
  },
  "devDependencies": {
    "jest": "^29.7.0"
  }
}

express and pg are needed to run the app in production. jest is only used for development and testing, so it's kept separate.

Common npm commands

npm install              # install everything listed in package.json
npm install express      # add express as a dependency
npm install -D jest      # add jest as a devDependency
npm run dev              # run the "dev" script
npx some-cli-tool        # run a package's command without installing it globally

npm install with no arguments sets up a project you just cloned. With a package name, it adds that package and updates package.json for you.

How it works

npm install <package> looks up the package (and every package it depends on) in the public npm registry, downloads them into node_modules, and adds an entry to package.json.

It also writes the exact resolved version of every package, direct and transitive, into package-lock.json. That lock file makes installs reproducible. Without it, two developers running npm install weeks apart could get different versions of the same "^4.19.0" dependency as new compatible releases come out.

Why does it exist?

Before package managers, reusing code meant downloading and copying files by hand, with no easy way to track versions or get updates. npm standardizes declaring what a project needs, finding existing packages, and installing everything (transitive dependencies included) reproducibly.

When to use it

Every real Node.js project uses npm. Even with one dependency, package.json documents what the project needs and scripts standardize how to run it.

When not to use it

The real question is when to skip adding a dependency. A package for something trivial you could write in a few lines (like checking if a number is even) is one more thing to update and trust, for little gain.

Common mistakes

  • Committing node_modules to git instead of adding it to .gitignore and letting npm install rebuild it from package.json.

  • Hand-editing package-lock.json, which npm manages and expects to match actual installs.

  • Installing a test or build tool as a regular dependency instead of a devDependency, bloating what ships to production.

Practice exercises

  1. Easy:

    Explain the difference between dependencies and devDependencies in package.json.

  2. Medium:

    A package.json has express under dependencies and jest under devDependencies. Name the command that installs only what's needed to run the app in production.

  3. Hard:

    Explain how two developers without a package-lock.json could get different versions of the same dependency, and how committing that file prevents it.

Interview questions

What is package.json?

A file describing a Node.js project: its name, dependencies and devDependencies, and shortcut commands under scripts.

What's the difference between dependencies and devDependencies?

dependencies are needed for the app to run in production; devDependencies are only needed while developing, like test runners or build tools.

What is package-lock.json for?

It records the exact resolved version of every installed package, direct and transitive, so future installs are reproducible instead of drifting as new compatible versions come out.

Why shouldn't `node_modules` be committed to version control, given that `npm install` can regenerate it?

It can be fully rebuilt from package.json (and package-lock.json) on any machine. Committing it bloats the repo with a huge derived folder that adds no information.

What does the caret (`^`) in `"express": "^4.19.0"` allow npm to install, and what does it forbid?

It allows any backward-compatible version under semantic versioning: any 4.x.x at or above 4.19.0. It forbids a new major version like 5.0.0, which may have breaking changes.

Two developers run `npm install` weeks apart on a project with only `"express": "^4.19.0"` in package.json and no lock file. Why might they get different versions?

The caret range allows any compatible 4.x.x, and new releases can be published between the two installs. Without a lock file pinning the version, each install takes the newest match at that moment.

How does committing package-lock.json solve the problem in the previous scenario?

The lock file records the exact versions resolved at install time, including every transitive dependency. When it's committed, npm install reuses those pinned versions instead of re-resolving the caret ranges.

Why is hand-editing package-lock.json considered a mistake, even though it's just a text file?

Its entries (exact versions, integrity hashes, the dependency tree) must stay consistent with each other and with package.json. npm manages it automatically, and manual edits can easily put it out of sync with what's installed or with package.json.

What does `npm install express` do differently from `npm install` with no arguments?

npm install alone installs everything already in package.json. npm install express also adds express as a new entry under dependencies, then installs it.

What does the `-D` flag do in `npm install -D jest`, and why does it matter which category a package ends up in?

-D installs the package as a devDependency. It matters because production-only installs (for deployment) skip devDependencies, so the wrong category changes what ships.

`jest` is accidentally installed as a regular dependency instead of a devDependency. What's the practical consequence in production?

A production install meant to skip devDependencies still pulls in jest and everything it depends on, bloating the deployed node_modules with a test tool the app never uses.

What does `npx some-cli-tool` let you do that installing the package first and then running it does not?

It runs the package's command directly (downloading it temporarily if needed) without adding it to the project or installing it globally. Handy for one-off or rarely used command-line tools.

What's the difference between what's read from package.json's scripts section and what running `node server.js` directly does?

A script like "dev": "node server.js" is just a named alias. npm run dev runs that exact command, so it behaves the same as typing it, under a shorter, standard name.

Why does npm need to resolve and download transitive dependencies, not just the ones listed directly in package.json?

Most packages depend on other packages. If npm only installed what your package.json lists, those packages would be missing code they need and would fail.

If dependencies includes express and devDependencies includes jest, what command installs only what's needed to run the app in production?

npm install --omit=dev (or the older npm install --production). It skips devDependencies and installs only regular dependencies.

Why does npm exist — what problem did developers have before package managers when reusing someone else's code?

Reusing code meant finding, downloading and copying files by hand, with no standard way to track versions, get updates, or include a dependency's own dependencies. npm standardizes declaring, finding and installing packages (and their transitive dependencies) reproducibly.

You clone a teammate's project and run `npm install`, but there's also a `.env` file needed with a `DATABASE_URL`. Why isn't `npm install` alone enough to run the app?

npm install only installs the code dependencies in package.json. It knows nothing about environment config like database connection strings, which are kept out of the package and version control (often in a gitignored .env) and must be set up separately.

What's the risk of adding a dependency for something trivial you could write yourself in a few lines, from a package-management point of view?

Every dependency must be trusted and kept updated, and it grows your install size and dependency tree. For something as small as checking if a number is even, that ongoing cost outweighs the tiny effort saved.

Why is package.json's scripts section useful even for a solo developer working alone?

It records exactly how to run, test or build the project, so you don't have to remember the underlying command, even months later.

What's recorded in package.json versus package-lock.json — are they redundant with each other?

package.json records the version ranges the developer chose for direct dependencies (like ^4.19.0). package-lock.json records the exact version installed for every package in the tree, direct and transitive. They serve different purposes and aren't redundant.