Skip to the workspace
Pebellslearn by building
Package Managers  ›  Lesson

npm and package.json

npm 14 minpackage.json records a project's dependencies, dev dependencies and scripts, and npm installs them from the registry into node_modules, which is never committed.

frontend / npm and package.json

Explore each scene at your own pace.

01 · The rule

The rule

npm and package.json

The bakery wants a photo carousel and a date picker for cake orders. You could write both from scratch, or use well-tested code other developers have already shared. Packages are that shared code, and a package manager downloads them, tracks them and keeps every teammate's copy the same. npm is the one that comes with Node.js.

The rule

package.json lists what the project needs; the package manager fetches it.

Start a project's manifest with:

npm init -y

This creates package.json, a small JSON file describing the project:

{
  "name": "crumb-site",
  "version": "1.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  },
  "dependencies": {
    "flatpickr": "^4.6.13"
  },
  "devDependencies": {
    "vite": "^5.4.0"
  }
}

Installing packages.

npm install flatpickr        # needed by the site itself
npm install -D vite          # needed only while developing
  • npm install <name> downloads the package from the npm registry, the big public library of packages, and adds it to dependencies.
  • -D (short for --save-dev) puts it under devDependencies instead: tools you need for building and testing, but which the finished site does not run.
  • The code lands in a node_modules folder, along with every package those packages depend on. That folder gets large quickly.

Recreating the setup. A teammate who clones the repository gets package.json but not node_modules, because node_modules belongs in .gitignore. They run one command:

npm install

With no name after it, npm install reads package.json and installs everything listed.

Scripts. The scripts section gives short names to common commands. npm run dev runs vite; npm run build runs vite build. A few names such as test and start also work without run. Scripts can use tools installed in node_modules directly, so nobody has to install them globally.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.