Learning Goals
3 minBy the end of this lesson you will be able to:
- Create a
package.jsonin a new folder withnpm init -y. - Install a package with
npm installand find it inside node_modules. - Use an installed package in a
.jsfile withrequire()and run it with Node. - Add an npm script and run it with
npm runornpm start.
Warm-Up · Read the Recipe Card
5 minLast lesson you ran a .js file from the terminal with node. Today every project keeps a small settings file called package.json. Here is a tiny one:
{
"name": "kuih-shop",
"scripts": {
"start": "node index.js"
}
}Predict together
- What single command does
npm startactually run? - If the file were named
app.jsinstead, what would you change?
Reveal
npm start looks up the "start" line inside "scripts" and runs whatever it finds — here that is node index.js. It is a nickname for a longer command. If the file were app.js, you would change the script to "node app.js". Scripts save you typing the same command over and over.
New Concept · npm, the Shop for Code
12 minThink of your phone. When you want a new feature, you do not build the app — you open the app store and install one. npm is exactly that, but for code. It is the Node Package Manager: an enormous shop of ready-made code called packages that anyone can install.
Every project you install packages into needs a package.json — think of it as the project's shopping list and ID card in one. It records the project's name, its version, and every package it depends on.
Making a package.json
You do not write it by hand. Inside your project folder, run this in the terminal:
$ npm init -yThe -y means “say yes to every question”, so npm fills in sensible defaults and writes this file for you:
{
"name": "my-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}The fields that matter to you today:
Who the project is
name — the project's name (lower-case, no spaces).
version — its version number, like 1.0.0.
description — one line saying what it does.
What the project does
scripts — nicknames for commands you run often.
dependencies — the packages this project installs (added for you when you install one).
When you install a package, its code lands in a folder called node_modules. That folder can hold thousands of files, so we never share it — it is listed in .gitignore and left out of Git. Anyone with your package.json can rebuild it with one command: npm install.
Why it matters
Real backends stand on hundreds of packages: web servers, date helpers, password tools. npm is how every one of them arrives. Learn npm now, and the rest of Level 3 — Express, Postgres tools, and more — installs the same way.
Worked Example · Install and Use dayjs
12 minFollow along on your own machine. Open the VS Code terminal with Terminal → New Terminal. We will build a tiny project that prints today's date in a friendly format.
Step 1 — Make a folder and start the project
Create a folder, move into it, then let npm set it up:
$ mkdir date-printer
$ cd date-printer
$ npm init -ynpm confirms it wrote the file:
Wrote to /date-printer/package.json:
{
"name": "date-printer",
"version": "1.0.0",
...
}Step 2 — Look at package.json
Open package.json in VS Code. Notice the name is date-printer and there is no dependencies field yet — you have not installed anything. That is about to change.
Step 3 — Install a package
dayjs is a tiny package for working with dates. Install it:
$ npm install dayjsadded 1 package in 2sTwo things changed. A node_modules folder appeared, and your package.json now has a dependencies field:
"dependencies": {
"dayjs": "^1.11.13"
}Step 4 — Use it in a file
Make a file called index.js. The require() function pulls an installed package into your code:
const dayjs = require('dayjs')
const today = dayjs().format('dddd, D MMMM YYYY')
console.log('Selamat datang! Today is ' + today)Run it with Node:
$ node index.jsSelamat datang! Today is Saturday, 4 July 2026Step 5 — Add a start script
Typing node index.js every time is a chore. Open package.json and add a start line inside scripts:
"scripts": {
"start": "node index.js"
}Now run the whole thing with a shorter command:
$ npm startSelamat datang! Today is Saturday, 4 July 2026What changed? You went from writing every line yourself to borrowing dayjs from npm, then gave your run command a nickname. Same result, far less typing.
Try It Yourself
13 minKeep working in your date-printer folder. Run scripts with npm run <name> and files with node index.js.
Task 1 — Add your own script
In
package.json, add a second script namedhellonext tostart:"scripts": { "start": "node index.js", "hello": "node index.js" }Save, then run it with
npm run hello. Notice thatstartis special (npm startworks), but your own scripts always need the wordrun.Task 2 — Install one more package
Install the tiny package chalk with
npm install chalk@4. It colours terminal text. Use it inindex.js:const chalk = require('chalk') console.log(chalk.green('Today looks bright, Aisyah!'))Run it and check the message prints in green.
Task 3 — Fill in the ID card
Open
package.json. Changenameto something of your own (lower-case, no spaces), bumpversionto1.1.0, and add a realdescriptionline that says what your project does. Save and checknpm startstill runs.
Mini-Challenge · Countdown to Hari Raya
8 minBuild a small project called countdown that uses dayjs to print how many days are left until a Malaysian holiday. dayjs has a .diff() method that counts the gap between two dates. Combine it with what you know from Level 2 about printing text.
It works if: running node index.js (or npm start) prints a single line with a whole number of days.
Reveal a sample answer
After npm init -y and npm install dayjs, this index.js does the job:
// index.js — days until Hari Raya for Faiz
const dayjs = require('dayjs')
const hariRaya = dayjs('2027-03-20')
const daysLeft = hariRaya.diff(dayjs(), 'day')
console.log('Only ' + daysLeft + ' days until Hari Raya!')Running it prints something like:
Only 259 days until Hari Raya!Your number depends on today's date, and you can swap in any holiday — Deepavali, Chinese New Year, or Christmas. The point is a package doing the hard date maths for you.
Recap
3 min- npm — the Node package manager — is a huge shop of ready-made code.
npm init -ycreates a package.json: your project's ID card and shopping list.npm install <package>copies code into node_modules and lists it underdependencies.- Bring a package into a file with
require('<package>'), then run it with Node. - An npm script is a command nickname — run it with
npm run <name>, ornpm startforstart.
New words
- npm — the Node Package Manager: the tool that installs packages.
- package.json — the file that describes your project and its packages.
- node_modules — the folder where installed package code lives (kept out of Git).
- Dependency — a package your project needs to run.
- Script — a named command shortcut inside
package.json.
Homework
4 min to brief · ~20 min to doRequired
- Make a new folder and run
npm init -yinside it. - Install one package —
dayjs,chalk@4, or any small one you like — then use it in anindex.jsfile and run it withnode index.js. Bring your package.json to next lesson (the file itself, not node_modules).
Optional stretch
- Look at the version in your
dependencies, like"dayjs": "^1.11.13". Find out what the^means — it is part of semantic versioning. Write one sentence explaining which updates it allows. - Install a package as a dev dependency with
npm install -D nodemon. Look at howpackage.jsonchanges — it lands under a newdevDependenciesfield instead ofdependencies.