Course:Internet & Tools/
Lesson

Every modern web project uses code written by other people, date formatting, HTTPWhat is http?The protocol browsers and servers use to exchange web pages, API data, and other resources, defining how requests and responses are formatted. requests, UI components, encryptionWhat is encryption?Scrambling data so only someone with the right key can read it, protecting information from being intercepted or stolen.. Without a tool to manage all of that, you would spend more time downloading and updating libraries than actually building your app. That is the problem package managers solve.

The manual alternative (and why it fails)

Before package managers, developers downloaded library files from websites and dropped them into their project folders. If Library A depended on Library B version 2.3, you had to figure that out yourself, download it, and hope nothing conflicted with Library C.

Manual workflow for 3 libraries:
1. Visit react.dev → download react.js
2. Visit lodash.com → download lodash.js
3. Visit axios-http.com → download axios.js
4. Discover React needs "scheduler" → download that too
5. A security fix is released → repeat everything

A typical React app has 800+ packages in its dependencyWhat is dependency?A piece of code written by someone else that your project needs to work. Think of it as a building block you import instead of writing yourself. tree. The manual approach is not just painful, it is genuinely impossible at that scale.

02

What npm does for you

npm is like an App Store for code. You tell it what you need, and it downloads the package plus every sub-dependencyWhat is dependency?A piece of code written by someone else that your project needs to work. Think of it as a building block you import instead of writing yourself., resolves version conflicts, and records exactly what was installed so your teammates get the same setup.

# Install a single package
npm install axios

# Install multiple packages at once
npm install react react-dom

# Install a tool only needed during development
npm install --save-dev typescript

# Install everything listed in package.json (after cloning a repo)
npm install

Every install command updates two files automatically: package.json (your declared dependencies) and package-lock.json (the exact resolved versions).

03

Where packages live

When you run npm install, packages land in a node_modules folder at the root of your project:

my-project/
├── node_modules/       ← all installed package code
│   ├── react/
│   ├── axios/
│   ├── lodash/
│   └── ... (hundreds more)
├── package.json        ← what you declared you need
└── package-lock.json   ← exact resolved versions

This folder is excluded from Git via .gitignore. Anyone cloning your repo runs npm install and gets the same packages, that is the entire point.

04

CDNWhat is cdn?Content Delivery Network - a network of servers around the world that caches your files and serves them from the location closest to the user, making pages load faster. links vs npm packages

You can load libraries via <script> tags from a CDN, and that works for quick experiments. But for real projects, npm packages win on every metric that matters:

FeatureCDN <script> tagnpm package
TypeScript types includedNoYes (usually)
Tree shaking (remove unused code)NoYes
Offline developmentNoYes
Version pinningFragile, CDN can changeExact via lockfile
Works with build tools (Vite, Webpack)LimitedFull support
Automatic sub-dependency resolutionNoYes
AI pitfall
When you ask ChatGPT or Claude to add a feature, they sometimes suggest installing 3-4 packages when one would suffice. For example, AI might suggest moment, dayjs, AND date-fns for date formatting, you only need one. Always check if a newly suggested package overlaps with something you already have.
05

npm alternatives

npm is not the only package manager. Two popular alternatives exist, and AI tools sometimes generate commands for the wrong one:

ManagerInstall commandLockfileKey difference
npmnpm installpackage-lock.jsonShips with Node.js, most widely used
yarnyarn addyarn.lockFaster installs via caching
pnpmpnpm addpnpm-lock.yamlSaves disk space by sharing packages

All three use the same package.json format and the same npm registryWhat is registry?A server that stores and distributes packages or container images - npm registry for JavaScript packages, Docker Hub for container images.. The differences are in speed, disk usage, and lockfileWhat is lockfile?An auto-generated file (like package-lock.json) that records the exact version of every installed package so every environment gets identical code. format. If you see a project with a yarn.lock, use yarn commands. If you see pnpm-lock.yaml, use pnpm. Otherwise, npm is the safe default.

06

Your first npm commands

These five commands cover 95% of what you will do day to day:

# Check that Node.js and npm are installed
node -v        # e.g., v20.11.0
npm -v         # e.g., 10.2.4

# Create a new project (generates package.json)
npm init -y

# Install a production dependency
npm install react

# Install a dev-only dependency
npm install --save-dev eslint

# Run a script defined in package.json
npm run dev
07

Quick reference

CommandWhat it does
npm init -yCreate a new package.json with defaults
npm install <pkg>Install a production dependency
npm install -D <pkg>Install a dev-only dependency
npm installInstall everything listed in package.json
npm updateUpdate packages to latest compatible versions
npm run <script>Run a script defined in package.json
npm lsShow installed packages as a tree
npm outdatedList packages with newer versions available
javascript
// package.json, the manifest of your project
{
  "name": "my-awesome-app",
  "version": "1.0.0",
  "description": "A great web application",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "vite",
    "build": "vite build",
    "test": "jest"
  },
  "dependencies": {
    "react": "^18.2.0",
    "axios": "^1.6.0"
  },
  "devDependencies": {
    "jest": "^29.0.0",
    "typescript": "^5.3.0"
  }
}