Skip to main content

๐Ÿš€ Day 2: Understanding npm in Node.js โ€“ Packages, package.json & Your First Project

Updated 5 min readMarkdown
Part of seriesNode.js Mastery: From Beginner to Production

๐Ÿ‘‹ 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.json

  • Understanding package-lock.json

  • Understanding node_modules

  • Installing 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.

  • 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.

๐Ÿ”— https://www.npmjs.com/

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_modules folder

  • Updates package.json

  • Generates 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_modules folder 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.json stores project information

  • package-lock.json locks dependency versions

  • node_modules stores installed packages

  • Local 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

No comments yet. Be the first to comment.

Node.js Mastery: From Beginner to Production

Part 2 of 5

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. ๐Ÿš€

Up next๐Ÿš€ Day 3: Node.js REPL Explained with Practical Examples | Read, Evaluate, Print, LoopNode.js Mastery Series by CodeWithPatil Learn how to use the Node.js REPL, execute JavaScript directly from the terminal, explore useful Node CLI commands, and boost your development workflow.