npm and packages
Overview
npm is the package manager that comes with Node.js. It downloads libraries other developers have published, so you don’t have to write everything yourself. The npm registry holds millions of packages: date libraries, chart libraries, testing tools, whole frameworks like React.
You’ll use npm even for browser projects, because modern front-end tools (bundlers, formatters, linters, frameworks) are themselves npm packages.
To use it, install Node.js (the LTS version) from nodejs.org. npm comes with it. Check both are there:
node --version
npm --versionStarting a project
npm init -yThis creates package.json, which describes your project and its dependencies. The -y accepts the default answers; you can edit the file afterwards.
{
"name": "my-project",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "node index.js"
}
}Add "type": "module" so Node treats your .js files as ES modules and import/export work.
Installing packages
npm install date-fns # a dependency your app uses
npm install -D prettier # a dev dependency: tools for building or testing{
"dependencies": { "date-fns": "^4.1.0" },
"devDependencies": { "prettier": "^3.3.0" }
}The code itself goes into the node_modules folder. Don’t commit that folder — anyone can recreate it with npm install. The package-lock.json file records exact versions so everyone gets the same ones.
Then import the package by its name:
import { format, addDays } from 'date-fns';
format(addDays(new Date(), 7), 'd MMM yyyy'); // a week from todayCommon commands
| Command | What it does |
|---|---|
npm install |
install everything listed in package.json (run after cloning a project) |
npm install <pkg> |
add a dependency |
npm install -D <pkg> |
add a dev dependency |
npm uninstall <pkg> |
remove one |
npm update |
update within the allowed version ranges |
npm outdated |
list packages with newer versions |
npm ci |
clean install exactly from the lock file (used on build servers) |
dependencies or devDependencies?
- dependencies — code your app needs while running: a date library, a charting library, React.
- devDependencies — tools used only while developing: Prettier, ESLint, a test runner, a bundler.
Version ranges
Package versions follow semantic versioning: MAJOR.MINOR.PATCH.
- PATCH (4.1.1) — bug fixes only.
- MINOR (4.2.0) — new features, nothing removed.
- MAJOR (5.0.0) — breaking changes; your code may need updating.
^4.1.0 means “4.1.0 or any newer 4.x version”. A change in the first number (5.0.0) may break things, so npm won’t pick it automatically. Upgrading a major version is a deliberate step: read the package’s changelog, then npm install date-fns@5.
Scripts
{
"scripts": {
"dev": "astro dev",
"build": "astro build",
"format": "prettier --write ."
}
}Run them with npm run dev, npm run build and so on (npm start and npm test work without run). Scripts give a project one agreed set of commands — a new teammate doesn’t need to know which tools you use, only npm run dev.
Tools installed in the project are available inside scripts by name, without a path.
npx
npx runs a package’s command without installing it permanently — handy for one-off tools and project generators:
npx prettier --check .
npx create-vite@latest my-appUsing packages in the browser
Browsers can’t read node_modules directly or understand bare names like 'date-fns'. A build tool solves this: it follows your imports, pulls in the package code, and bundles everything into files the browser can load. Vite is a popular, fast choice:
npm create vite@latest my-app
cd my-app
npm install
npm run devFor a quick experiment without a build step, you can import from a CDN instead: import { format } from 'https://esm.sh/date-fns';.
Check before you install. Look at a package’s downloads, recent updates and size. Every dependency is code you’re trusting.
A few good habits:
- Prefer well-maintained packages with many users.
- Don’t install a package for something a few lines of code can do.
- Run
npm auditto check for known security problems. - Watch for typos in names — fake packages sometimes imitate popular ones.
Set up a project
In an empty folder, create a project with npm init -y, install date-fns, and add a start script that runs node index.js. Write index.js so it prints today’s date formatted as d MMMM yyyy, then run npm start.
// index.js
// import format from date-fns and print today's date// package.json — add "type": "module" and
// "scripts": { "start": "node index.js" }
// index.js
import { format } from 'date-fns';
console.log(format(new Date(), 'd MMMM yyyy'));Summary
- npm installs packages into
node_modulesand records them inpackage.json. dependenciesship with your app;devDependenciesare only for building and testing.- Commit
package.jsonandpackage-lock.json, nevernode_modules. - Versions follow MAJOR.MINOR.PATCH;
^allows new minor and patch versions. - Scripts in
package.jsongive your project short, shared commands;npxruns tools without installing them.