Node.js Tutorial menu

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

bash
npm init -y

Example

json
{
  "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

bash
npm install dayjs

Three things happen:

  1. The package is downloaded into a node_modules folder
  2. It is added to dependencies in package.json
  3. Exact versions are saved in package-lock.json

Now use it:

Example

js
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"));
Output
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

CommandWhat it does
npm installInstalls everything listed in package.json
npm install expressAdds a package (short: npm i express)
npm install -D eslintAdds a development-only package
npm uninstall dayjsRemoves a package
npm updateUpdates packages within the allowed versions
npm outdatedLists packages that have newer versions
npm run devRuns the script named dev
npx some-toolRuns 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

json
"dependencies": {
  "express": "^5.1.0"
},
"devDependencies": {
  "eslint": "^9.30.0"
}

Version Numbers

Versions follow semantic versioning: MAJOR.MINOR.PATCH, such as 5.1.0.

PartChanges whenExample
MAJORBreaking changes4.x to 5.0
MINORNew features, nothing broken5.0 to 5.1
PATCHBug fixes5.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

json
"scripts": {
  "start": "node index.js",
  "dev": "node --watch index.js"
}

Example

bash
npm run dev
npm start

start 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

text
node_modules
.env

Anyone 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?

Learn Node.js with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp