← Back to the Build Your Homepage series
🟒
EPISODE 03
package.json Β· npm install Β· scripts Β· semver

npm & Packages

Use npm to manage dependencies, run scripts, and publish your own packages. Understand semver and lockfiles.

npmpackage.jsonsemverscripts
Duration
⏱ About 1.5 hours
Level
πŸ“Š Beginner+
Prerequisite
🎯 node-01
OUTCOME
Create a Node project that pulls in npm packages and runs custom scripts

What you'll learn

  • 1Initialize a package.json
  • 2Install, update, and remove dependencies
  • 3Define and run npm scripts
  • 4Read semver ranges (^, ~, exact)

1. Start a Project

bash
mkdir my-app && cd my-app
npm init -y          # creates package.json
npm install chalk    # adds to dependencies
npm install --save-dev nodemon   # devDependencies

2. package.json

json
{
  "name": "my-app",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "node index.js",
    "dev":   "nodemon index.js",
    "test":  "node --test"
  },
  "dependencies": { "chalk": "^5.3.0" },
  "devDependencies": { "nodemon": "^3.0.0" }
}
bash
npm run dev          # runs the "dev" script
npm start            # 'start' is a special shortcut

3. Semver

RangeMeaning
^1.2.3Compatible: >=1.2.3, <2.0.0 (most common default)
~1.2.3Patch: >=1.2.3, <1.3.0
1.2.3Exact
>=1.2.3Anything newer
*Any version (avoid)

4. The Lockfile

  • npm install also writes package-lock.json with the exact resolved versions
  • Commit the lockfile β€” it makes installs reproducible across machines
  • Use npm ci in CI for fast, exact installs from the lockfile
Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub β†—