npm Aur Package.json — Dependencies Manage Karna
npm is not just a package manager. It's the world's largest software registry.
npm stands for Node Package Manager. It comes automatically with Node.js — when you install Node, npm is included. No separate installation needed.
Think of npm as Hyderabad's biggest chai ingredient market. You want sugar? Go to the sugar shop. You want elaichi? Go to the spice shop. You want ginger? Adrak wala shop. npm is one giant market where every ingredient (package) is available, and you can bring them into your project with one command.
Why do we need npm? Imagine building a chai shop from scratch every time — growing your own tea leaves, making your own cups, rolling your own sugar. That's insane. Instead, you reuse existing packages: Express for HTTP, Lodash for utilities, Mongoose for databases, etc.
npm key statistics:
- 2+ million packages available (biggest code registry in the world)
- ~100 billion downloads per month
- Every Node.js developer uses it daily
- npm, Inc. was acquired by GitHub (Microsoft) in 2020
Checking npm version:
node -v # Check Node.js version
npm -v # Check npm version
# If you have Node, you have npm. Guaranteed.
If you have Node.js installed, you have npm. Simple as that bhai.
package.json is the most important file in any Node.js project. Think of it as your chai shop's register — it records everything:
- name — Shop ka naam (e.g., "chai-point")
- version — Recipe version (e.g., "1.0.0")
- description — Shop kya bechti hai
- main — Kaunsa file shop kholti hai (entry point)
- scripts — Bawarchi ke shortcuts (npm start, npm test)
- dependencies — Sab ingredients jo chai banane chahiye
- devDependencies — Chef ke tools (testing, building)
Creating package.json: Run npm init and answer the questions, or use npm init -y for defaults:
npm init -y # Creates package.json with all defaults
# Output: Created package.json in current directory
Here's what a typical package.json looks like:
{
"name": "hyderabadi-biryani-api",
"version": "1.0.0",
"description": "Biryani delivery backend API",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "node --watch server.js"
},
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
Each field serves a purpose. The scripts section is especially powerful — it lets you create shortcuts for common commands. Instead of typing node server.js, you just type npm start.
npm install is your ingredient ordering system. Let's see all the ways to install packages:
1. Regular dependency (production use):
npm install express
# Shortcut: npm i express
# Adds to "dependencies" in package.json
# Eg: Express is needed for the API to run in production
2. Dev dependency (development only):
npm install --save-dev nodemon
# Shortcut: npm i -D nodemon
# Adds to "devDependencies"
# Eg: Nodemon restarts server during dev — not needed in production
3. Global installation (available everywhere):
npm install -g typescript
# Installs globally, not in current project
# Use: npx or just the command directly
4. Specific version:
npm install express@4.18.0 # Exact version
npm install express@^4.0.0 # Compatible with version 4.x
5. Uninstall:
npm uninstall express # Removes from node_modules + package.json
Understanding version numbers (SemVer):
npm uses Semantic Versioning (SemVer): MAJOR.MINOR.PATCH
- Major = Breaking changes (Express v4 → v5: API changes)
- Minor = New features, backward compatible (v4.18.0 → v4.19.0)
- Patch = Bug fixes (v4.18.0 → v4.18.1)
Version prefixes in package.json:
^4.18.0— Compatible with 4.x.x (allows minor + patch updates)~4.18.0— Only patch updates (4.18.x)4.18.0— Exact version only (no updates)*— Any version (dangerous — never use in production!)
Think of it like biryani ingredients: you trust your regular supplier (^) for small changes, but for exact measurement of a specific spice (~), you want it exactly right.
node_modules is the kitchen storage of your project. When you npm install, all downloaded packages go here. It's huge, messy, and you NEVER touch it manually.
# This folder can be 100MB+ for a small project
# NEVER commit node_modules to Git!
npm install express # Creates node_modules/express + 30+ sub-dependencies
Why is node_modules so big? Because each package has its own dependencies. Express depends on body-parser, body-parser depends on bytes, etc. This is called the dependency tree. npm installs ALL of them recursively.
package-lock.json is the exact recipe book. While package.json says "Express ^4.18.2" (any minor version), package-lock.json records the EXACT version of every package in the tree:
// package-lock.json (auto-generated, do not edit)
{
"name": "hyderabadi-biryani-api",
"lockfileVersion": 3,
"packages": {
"node_modules/express": {
"version": "4.18.2", // Exact version locked
"resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz"
}
}
}
Why is package-lock.json important? Imagine your chai recipe says "add sugar" — but different chai walas use different amounts. package-lock.json is the exact recipe: "add 2 teaspoons of sugar brand X." It ensures every developer and every deployment gets the EXACT same dependencies.
Always commit package-lock.json to Git! It ensures reproducible builds across machines.
.gitignore — Protect your kitchen:
# .gitignore — ALWAYS add these
node_modules/
.env
*.log
node_modules is generated from package.json — no need to store it in Git. Just run npm install after cloning and you get everything back.
npm scripts are your bawarchi shortcuts. Define them in package.json scripts section, and run them with npm run <name>:
// package.json
{
"scripts": {
"start": "node server.js",
"dev": "node --watch server.js",
"test": "node --test",
"lint": "eslint ." // Custom script
}
}
# Run scripts:
npm start # Runs: node server.js
npm run dev # Runs: node --watch server.js
npm test # Runs: node --test
npm run lint # Runs: eslint .
Special scripts: start and test can be run without run. All others need npm run.
Pre and Post hooks: npm automatically runs pre/post scripts if they exist:
{
"scripts": {
"prestart": "echo 'Starting server...',
"start": "node server.js",
"poststart": "echo 'Server started!'"
}
}
# npm start runs: prestart → start → poststart
npx — Guest Chef Bulao
npx lets you run packages without installing them. It's like calling a guest chef who brings their own tools:
npx create-react-app my-app # Runs without global install
npx cowsay "Chai piyoge?" # Fun package, one-time use
npx http-server # Start a quick HTTP server
npx is perfect for one-time tools (scaffolding, generators, fun utilities) that you don't want to pollute your global packages with.
Real Hyderabad workflow:
# 1. Start a new Node.js project
mkdir biryani-api
cd biryani-api
npm init -y
# 2. Install Express
npm install express
# 3. Install dev tools
npm install -D nodemon
# 4. Create start script in package.json
# "scripts": { "dev": "nodemon server.js" }
# 5. Run the server
npm run dev
That's it bhai! You have a fully managed Node.js project. npm handles all the dependency chaos so you can focus on writing code, not managing packages.
Key Takeaways
- ✅ npm = Node Package Manager — comes with Node.js automatically.
- ✅ package.json is the project manifest — name, version, deps, scripts sab yahan.
- ✅ npm install adds to dependencies (production), npm install -D adds to devDependencies (dev only).
- ✅ node_modules = kitchen storage — never touch it, never commit it to Git.
- ✅ package-lock.json = exact recipe book — ALWAYS commit it to Git.
- ✅ SemVer: Major.Minor.Patch (^ allows minor updates, ~ allows only patches).
- ✅ npx runs packages without installing them — like hiring a guest chef.
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login