Skip to content
ClapelloNotes by Barath
ReactCh 02 · 1/6
Beginner4 min readUpdated

React / 04

npm and package.json

TL;DR

npm downloads the code your app depends on. package.json is the list of what your project needs and the commands that run it.

What npm is

npm is the package manager that comes with Node.js. Install Node and you get the npm command too.

  • A package is a folder of reusable code with a package.json inside, like react or vite.
  • The npm registry (npmjs.com) is the huge public library where packages are published.
  • The npm command downloads packages from the registry into your project.

Start a project

mkdir tasty-bites && cd tasty-bites
npm init -y

npm init creates package.json. The -y flag accepts all the default answers. In practice you'll usually let a tool such as npm create vite@latest make it for you (later in this chapter), but it's the same file.

Install packages

npm install react react-dom   # the app needs these to run
npm install -D vite           # a tool you only need while developing

you type

npm install react
npm downloads from the registry (npmjs.com)

1 · the wish list

package.json

"react": "^19.3.1"

a version range. You commit it.

2 · the receipt

package-lock.json

"version": "19.3.1"

exact versions of everything. You commit it.

3 · the goods

node_modules/

react/ …and friends

the actual code. Never commit it.

One command, three results: package.json records what you asked for, the lockfile records exactly what you got, node_modules holds the code.

After those two commands, package.json looks like this:

package.json
{
  "name": "tasty-bites",
  "version": "1.0.0",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  },
  "dependencies": {
    "react": "^19.3.1",
    "react-dom": "^19.3.1"
  },
  "devDependencies": {
    "vite": "^8.3.6"
  }
}

(The scripts were added by hand. The exact version numbers will be whatever is newest when you install.)

dependencies vs devDependencies

dependenciesdevDependencies
What goes hereCode your app runsTools for building, testing, linting
Examplesreact, react-dom, react-routervite, parcel, eslint, vitest
How to addnpm install reactnpm install -D vite (-D = --save-dev)

npm install with no package name installs both lists. A server can skip the dev tools with npm install --omit=dev.

Scripts: the project's commands

The scripts field gives names to commands:

npm run dev     # runs "vite"
npm run build   # runs "vite build"
npm start       # shortcut for npm run start
npm test        # shortcut for npm run test

Inside a script, the commands from installed packages (in node_modules/.bin) just work. That's why "dev": "vite" runs without installing Vite globally.

npx: run a package's command once

npx runs a command from a package without writing a script:

npx vite                # uses the copy in node_modules
npx parcel index.html   # same idea with Parcel

If the package isn't installed, npx offers to download it temporarily and run it. npm create vite@latest works the same way: it runs the create-vite package to scaffold a project.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.