NPM & package.json
Tap into millions of free packages and manage your project with npm — the world’s largest software library.
What you will learn
- Initialise a project with package.json
- Install and use packages
- Understand dependencies and scripts
Start a project
npm (Node Package Manager) comes free with Node. It is like an app store for code: millions of ready-made packages you can drop into your project in seconds. Every npm project starts with a package.json file — your project’s ID card, listing its name, version, scripts and the packages it depends on.
Using a package is a simple three-step flow:
- Create package.json with
npm init -y— this is the file that will remember every package you add. - Install a package with
npm install <name>— npm downloads it into anode_modulesfolder and records it in package.json. - Use it in your code with
require("<name>"), exactly like a built-in module.
npm init -y # create package.json
npm install dayjs # install a package (adds it to dependencies)Note: Output:
Wrote to /your/project/package.json
...
added 1 package in 1s
npm init -y creates package.json instantly (the -y means "yes to all the default questions"). npm install dayjs then downloads the popular dayjs date library, drops its code into node_modules/, and adds a line for it under "dependencies" in package.json so the project remembers it.
Use an installed package
Once installed, you bring the package in with require — the same way you imported your own modules, but using just its name (no ./ ), because it lives in node_modules:
// app.js
const dayjs = require("dayjs");
console.log("Today is", dayjs().format("DD MMM YYYY"));Note: Output:
Today is 07 Jun 2026
require("dayjs") loads the installed library. Calling dayjs() makes an object for today’s date, and .format("DD MMM YYYY") turns it into a tidy string — DD is the day, MMM the short month name, YYYY the four-digit year. You wrote three lines and got date formatting that would take many lines by hand.
What gets created
| Thing | What it is |
|---|---|
package.json | Project info + list of dependencies |
package-lock.json | Exact versions installed (do not edit) |
node_modules/ | The downloaded package code (never commit it) |
Watch out: Never commit node_modules/ to git — it is huge and rebuildable. Add it to .gitignore. Anyone can recreate it with npm install using your package.json.
Scripts
Typing long commands again and again is tedious. Scripts let you save a command under a short name inside package.json (in the "scripts" section), then run it with npm run <name>. The dev tool nodemon auto-restarts your server every time you save a file.
// package.json
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
}Note: Output:
(no output — this is configuration)
This adds two shortcuts. Now npm start runs node app.js (the start name is special, so you can drop the run), and npm run dev runs nodemon app.js. You type a short, memorable command and npm runs the long one for you.
Q. Which file lists your project’s dependencies?
✍️ Practice
- Run
npm init -y, install a package, and use it in a script. - Add a
"start"script to package.json and run it withnpm start.
🏠 Homework
- Create a project, install
nodemonas a dev dependency, and add adevscript that uses it.