π¦ Day 5: Understanding NPM, Creating package.json, and Installing Dependencies in Node.js

Table of contents
- π¨βπ« What Youβll Learn Today:
- π What is NPM?
- π What is package.json?
- π οΈ How to Create a package.json File
- π₯ Installing Packages (Dependencies)
- π¦ Installing Dev Dependencies
- β Uninstalling a Package
- π Real-Life Project Example
- π§° NPM Scripts β Running Custom Commands
- π§ Important Files Created:
- β FAQs β Common Doubts
- π Practice Tasks
- β Summary
- π Final Output:
- π Step-by-Step Instructions
- π Bonus for Students
- β Summary of What Students Learn
- π Ideal Use for Class
π¨βπ« What Youβll Learn Today:
What is NPM?
What is
package.json
and why itβs importantHow to create a
package.json
fileHow to install, uninstall, and use packages (dependencies)
Real-life use cases
Common FAQs
π What is NPM?
NPM stands for Node Package Manager.
It is a tool that comes with Node.js and helps us:
Install libraries or tools (called "packages")
Manage project dependencies
Run custom scripts
π Think of NPM like Play Store but for backend tools, packages, and code utilities.
π How to Check if NPM is Installed?
Open your terminal and run:
npm -v
Youβll see a version number like:
10.5.0
π What is package.json
?
package.json
is a configuration file that stores:
Project name, version, and description
Installed packages (dependencies)
Custom scripts (like
npm start
)Project metadata (author, license, etc.)
π§ Why is it important?
β It allows others (or you) to download your code and just run:
npm install
β¦to get everything your project needs.
π οΈ How to Create a package.json
File
Open terminal in your project folder and run:
npm init
This will ask you some questions (name, version, entry point, etc.).
If you want to skip all questions and use default values:
npm init -y
It will instantly create a file like this:
{
"name": "my-project",
"version": "1.0.0",
"main": "index.js",
"license": "ISC"
}
Now your project is officially NPM-ready!
π₯ Installing Packages (Dependencies)
Letβs say you want to use the chalk
package to color your console output.
Step 1: Install the package
npm install chalk
This will:
Add a folder called
node_modules
(where the package is saved)Update
package.json
and createpackage-lock.json
Step 2: Use the package in your code
const chalk = require('chalk');
console.log(chalk.green("Success! Your text is green now."));
β Output: Green-colored message in your terminal.
π¦ Installing Dev Dependencies
Some packages are only needed during development (like nodemon
).
Install them with --save-dev
:
npm install nodemon --save-dev
π These go under "devDependencies"
in package.json
.
β Uninstalling a Package
If you no longer need a package:
npm uninstall chalk
It removes the package from node_modules
and updates package.json
.
π Real-Life Project Example
Project Goal: Create a simple terminal tool that prints messages in colors.
Step-by-step:
mkdir color-printer
cd color-printer
npm init -y
npm install chalk
index.js
const chalk = require('chalk');
console.log(chalk.blue("Hello World in Blue"));
console.log(chalk.bold.red("Error! Something went wrong."));
Now run:
node index.js
β Your terminal shows styled, colorful messages. This is useful in CLI tools, loggers, test results, etc.
π§° NPM Scripts β Running Custom Commands
Inside package.json
, you can define custom scripts:
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
Now run:
npm run start
npm run dev
β This saves time and avoids typing long commands.
π§ Important Files Created:
File | Purpose |
package.json | Project metadata and dependencies |
package-lock.json | Exact version lock to avoid bugs |
node_modules/ | Folder where all installed packages are saved |
β FAQs β Common Doubts
1) What is the difference between npm install chalk
and npm install chalk --save-dev
?
Command | Use For | Saved In |
npm install chalk | For actual project use | dependencies |
npm install chalk --save-dev | For dev-only tools (like nodemon) | devDependencies |
2) What happens if I delete node_modules
?
β Youβll lose all packages.
β
But you can run npm install
again, and it will reinstall everything using package.json
.
3) Whatβs the use of package-lock.json
?
It locks exact versions of installed packages so that everyone using your code gets the same result.
β
It's auto-generated. Do not delete it.
4) Can we manually install a specific version of a package?
Yes. Use @version
like:
npm install chalk@4.1.0
Useful when:
You need compatibility with older code
The latest version breaks something
5) What is the use of nodemon
?
It restarts your Node app automatically when files change. Great for development.
Install as dev dependency:
npm install nodemon --save-dev
Add script in package.json
:
"scripts": {
"dev": "nodemon index.js"
}
Then run:
npm run dev
π Practice Tasks
Create a new folder
my-app
, initializepackage.json
Install the
chalk
packageCreate a file
app.js
and print a colorful messageUninstall a package and reinstall it
Add a custom script called
hello
that prints a message usingnode app.js
β Summary
Concept | Description |
NPM | Node's package manager |
package.json | Project info + dependencies list |
npm install | Adds external libraries |
npm uninstall | Removes libraries |
NPM Scripts | Custom shortcut commands |
Real Use | Building tools, CLI apps, backend utilities |
You're now ready to build real backend projects using NPM and external packages π
β Project Title: "Colorful Message Generator" (Using NPM + Express + Chalk)
π§ Concepts Covered:
NPM initialization (
package.json
)Installing dependencies (
express
,chalk
)Running Node.js backend server
Sending color-coded responses in browser
π Final Output:
A browser-based webpage (localhost) that shows colorful success or error messages styled using chalk
.
π Step-by-Step Instructions
π 1. Create Project Folder
mkdir colorful-server
cd colorful-server
π¦ 2. Initialize NPM
npm init -y
Creates your package.json
file.
π₯ 3. Install Required Packages
npm install express chalk
express
β to create web serverchalk
β to color server logs (console)
π 4. Create a File: index.js
const express = require('express');
const chalk = require('chalk');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
console.log(chalk.green("β Home route visited successfully"));
res.send('<h2 style="color:green;">Welcome to the Colorful Server!</h2>');
});
app.get('/error', (req, res) => {
console.log(chalk.red("β Error route triggered"));
res.send('<h2 style="color:red;">Oops! Something went wrong.</h2>');
});
app.listen(PORT, () => {
console.log(chalk.blueBright(`π Server is running on http://localhost:${PORT}`));
});
βΆ 5. Run Your Server
node index.js
β Output on console:
π Server is running on http://localhost:3000
π 6. Open in Browser:
Go to: http://localhost:3000 β Green Welcome Message
Go to: http://localhost:3000/error β Red Error Message
And in terminal:
- Logs will show with green or red messages (from chalk)
π Bonus for Students
You can create more routes like:
app.get('/info', (req, res) => {
console.log(chalk.cyan("βΉ Info route hit"));
res.send('<h2 style="color:blue;">This is an informational message.</h2>');
});
β Summary of What Students Learn
Feature | Learning Outcome |
npm init -y | Create package.json |
npm install | Install express and chalk |
require() usage | Use installed dependencies in code |
Express server | Backend development intro |
Chalk in terminal | Beautify logs (real project feel) |
Live browser testing | See real-time output via localhost |
π Ideal Use for Class
Students understand NPM dependencies practically
No need to learn frontend for now β just use HTML in
res.send()
Easily explain the difference between terminal output and browser output
Would you like me to prepare:
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: π In-depth explorations of emerging technologies π‘ Practical tutorials and how-to guides π§Insights on software development best practices πReviews of the latest tools and frameworks π‘ Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letβs connect and grow together! π