๐ Day 2: Understanding npm in Node.js โ Packages, package.json & Your First Project
๐ Welcome Back, Developers!
In Day 1, we learned what Node.js is, why it was created, how to install it, and how to run our first JavaScript program.
Today, we're going to explore npm (Node Package Manager), one of the most essential tools in the Node.js ecosystem.
๐ What You'll Learn
What npm is
Why npm is important
What the npm Registry is
Creating your first project
Understanding
package.jsonUnderstanding
package-lock.jsonUnderstanding
node_modulesInstalling packages
Local vs Global packages
Useful npm commands
๐ฆ What is npm?
npm (Node Package Manager) is the default package manager that comes bundled with Node.js.
It helps developers install, update, remove, and manage reusable JavaScript packages.
๐ก Think of npm as the App Store for JavaScript libraries.
Instead of building everything from scratch, developers can use community-created packages to speed up development.
Popular npm Packages
Express
Mongoose
dotenv
bcrypt
axios
chalk
๐ค Why Do We Need npm?
Without npm, developers would have to write common functionalities from scratch for every project.
npm helps you:
โ Save development time
โ Reuse trusted libraries
โ Manage dependencies efficiently
โ Keep projects organized
โ Access millions of open-source packages
๐ What is the npm Registry?
The npm Registry is the official online repository where developers publish and share packages.
Installing a package is simple:
npm install express
npm automatically downloads the package from the registry and adds it to your project.
๐ Creating Your First Project
Create a new directory:
mkdir nodejs-npm-demo
cd nodejs-npm-demo
Initialize npm:
npm init
Or skip the setup questions:
npm init -y
This command creates a file called:
package.json
๐ Understanding package.json
The package.json file is the heart of every Node.js project.
Example
{
"name": "nodejs-npm-demo",
"version": "1.0.0",
"description": "Learning npm with CodeWithPatil",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "CodeWithPatil",
"license": "ISC"
}
Important Properties
name โ Project name
version โ Current version
description โ Project summary
main โ Entry file
scripts โ Custom commands
author โ Project author
license โ License information
Example Script
"scripts": {
"start": "node index.js"
}
Run it using:
npm start
Instead of:
node index.js
๐ Creating Your First index.js File
Create an index.js file:
console.log("Welcome to CodeWithPatil ๐");
console.log("Learning npm Step by Step.");
Run:
node index.js
Output:
Welcome to CodeWithPatil ๐
Learning npm Step by Step.
๐ฆ Installing Your First Package
Let's install Chalk, a package used for colorful terminal output.
npm install chalk
npm automatically:
Downloads the package
Creates the
node_modulesfolderUpdates
package.jsonGenerates
package-lock.json
๐ What is node_modules?
The node_modules folder contains all installed packages and their dependencies.
Example:
project/
โโโ node_modules/
โ ๏ธ Never commit the
node_modulesfolder to GitHub.
Add it to .gitignore:
node_modules/
๐ What is package-lock.json?
The package-lock.json file stores the exact versions of installed dependencies.
This ensures every developer working on the project installs the same package versions.
Benefits
โ Consistent builds
โ Fewer bugs caused by version differences
โ Reliable deployments
Keep this file in version control and avoid deleting it unnecessarily.
๐ Local vs Global Packages
Local Installation
Installed only within the current project.
npm install express
Use local packages for application dependencies.
Global Installation
Installed system-wide.
npm install -g nodemon
Use global packages for CLI and development tools.
Examples:
nodemon
typescript
eslint
๐ Useful npm Commands
npm init
npm init -y
npm install
npm uninstall
npm update
npm list
npm outdated
npm start
npm -v
๐ฏ Hands-on Example
Install Chalk:
npm install chalk
Create a file and add:
const chalk = require("chalk");
console.log(chalk.green("Welcome to CodeWithPatil ๐"));
console.log(chalk.blue("Learning npm is easy!"));
Output:
Welcome to CodeWithPatil ๐
Learning npm is easy!
โ Key Takeaways
npm comes bundled with Node.js
npm manages packages and dependencies
package.jsonstores project informationpackage-lock.jsonlocks dependency versionsnode_modulesstores installed packagesLocal packages are project-specific
Global packages are system-wide tools
๐ What's Next?
In Day 3, we'll learn about Modules in Node.js, including:
What are Modules?
CommonJS Modules
ES Modules
Import vs Require
Exporting Modules
Creating Custom Modules
Stay tuned! ๐
๐ Follow CodeWithPatil
If you enjoy practical tutorials on Backend Development, Node.js, TypeScript, GraphQL, Cloud Computing, DevOps, AWS, and AI for Developers, follow CodeWithPatil for real-world, project-based learning.
๐ฌ Feedback & Support
Have a question, found an issue, or have suggestions for future topics?
Leave a comment belowโI'd love to hear from you. Your feedback helps improve future articles and supports the learning community.
Happy Coding! ๐
About the author
- # Hi, I'm Sumit Patil ๐ a Senior Backend Software Engineer with 7+ years of experience designing scalable backend systems and cloud-native applications. I specialize in Node.js, NestJS, TypeScript, GraphQL, PostgreSQL, MongoDB, AWS, Microservices, and DevOps. ๐ Smart Gujarat for New India Hackathon Winner
Comments
Join the discussionNo comments yet. Be the first to comment.
Node.js Mastery: From Beginner to Production
Welcome to the Node.js Mastery series by CodeWithPatil. Master Node.js from the basics to advanced concepts through practical, project-based learning and Build scalable, real-world backend applications with CodeWithPatil. ๐

