Node.js npm
npm is the package manager for Node.js. It installs code that other developers have shared, so you do not have to write everything yourself.
What is npm?
- npm stands for Node Package Manager
- It is installed together with Node.js
- The npm registry has millions of free packages (libraries and tools)
- It also runs your project's scripts, like starting a server or running tests
Creating package.json
Every Node.js project has a package.json file. It stores the project's name, its scripts and the packages it needs. Create one with:
Example
npm init -yExample
{
"name": "school-api",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "ISC"
}-y accepts all the default answers. You can edit the file later.
Installing a Package
Example
npm install dayjsThree things happen:
- The package is downloaded into a
node_modulesfolder - It is added to
dependenciesinpackage.json - Exact versions are saved in
package-lock.json
Now use it:
Example
import dayjs from "dayjs";
const admission = dayjs("2026-10-15");
console.log("Admission:", admission.format("DD MMM YYYY"));
console.log("Course ends:", admission.add(6, "month").format("DD MMM YYYY"));Admission: 15 Oct 2026 Course ends: 15 Apr 2027
(This uses import, so add "type": "module" to package.json as shown in the Modules lesson.)
Common npm Commands
| Command | What it does |
|---|---|
npm install | Installs everything listed in package.json |
npm install express | Adds a package (short: npm i express) |
npm install -D eslint | Adds a development-only package |
npm uninstall dayjs | Removes a package |
npm update | Updates packages within the allowed versions |
npm outdated | Lists packages that have newer versions |
npm run dev | Runs the script named dev |
npx some-tool | Runs a package's command without installing it globally |
dependencies and devDependencies
- dependencies are needed to run the app, such as
express - devDependencies are only needed while developing, such as testing or linting tools
Example
"dependencies": {
"express": "^5.1.0"
},
"devDependencies": {
"eslint": "^9.30.0"
}Version Numbers
Versions follow semantic versioning: MAJOR.MINOR.PATCH, such as 5.1.0.
| Part | Changes when | Example |
|---|---|---|
| MAJOR | Breaking changes | 4.x to 5.0 |
| MINOR | New features, nothing broken | 5.0 to 5.1 |
| PATCH | Bug fixes | 5.1.0 to 5.1.1 |
The ^ in ^5.1.0 means "any 5.x version from 5.1.0 up". The ~ in ~5.1.0 means "only 5.1.x patches".
Scripts
Add your own commands to the scripts section:
Example
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js"
}Example
npm run dev
npm startstart and test are special: you can run them without run.
node_modules and Git
The node_modules folder can be very large. Never upload it to GitHub or send it to a client. Add it to .gitignore:
Example
node_modules
.envAnyone who downloads your project runs npm install to get the same packages, using package-lock.json for the exact versions.
Before installing a package, check how many weekly downloads it has and when it was last updated. Popular, maintained packages are safer.
Exercise
Exercise
Which file records the exact versions of every installed package?
