Basic Node.js Commands

 


Project Start Karne Ke Liye:

bash
# Naya project initialize karo
npm init

# Ye interactive mode hai - step by step puchenga
npm init -y                 # Saare defaults accept karo

Dependencies Install Karne Ke Liye:

bash
# Regular packages install karo
npm install express         # Express framework add karo
npm install mongoose        # MongoDB ke liye

# Development dependencies (testing, building ke liye)
npm install --save-dev nodemon   # Auto-restart ke liye
npm install -D jest         # Testing framework

# Global packages install karo
npm install -g nodemon      # Saare projects mein use kar sakte hain

Package.json Scripts (Bahut Important):

package.json ke andar:

json
{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",          # Development mode
    "test": "jest",                     # Tests run karo
    "build": "webpack --mode production" # Production build banao
  }
}

Inhe run karne ke liye:

bash
npm start          # Production mein run karo
npm run dev        # Development mode (with auto-restart)
npm run test       # Tests execute karo
npm run build      # Build process start karo

Common Development Commands

Dependencies Manage Karna:

bash
# Package remove karna
npm uninstall express

# Outdated packages check karna
npm outdated

# Packages update karna
npm update

# Saari packages latest version mein update karo
npm install -g npm-check-updates
ncu -u
npm install

Node Project Run Karna:

bash
# Simple file run karo
node app.js

# Environment variables ke saath run karo
NODE_ENV=production node app.js

# Debug mode mein run karo
node --inspect app.js

NPM Specific Commands

Version Management:

bash
# Package ka version check karo
npm version

# Version bump karo
npm version patch    # Small changes (1.0.0 → 1.0.1)
npm version minor    # New features (1.0.0 → 1.1.0)  
npm version major    # Breaking changes (1.0.0 → 2.0.0)

Security aur Maintenance:

bash
# Security vulnerabilities check karo
npm audit

# Automatic fix karo
npm audit fix

# Detailed audit report
npm audit --json

Useful Global Packages

bash
# Development ke liye helpful tools
npm install -g http-server    # Local static server
npm install -g concurrently   # Multiple commands ek saath run karo
npm install -g rimraf         # Cross-platform file delete

Real Project Example

Typical workflow:

bash
# Step 1: Project folder banao
mkdir my-node-app
cd my-node-app

# Step 2: Initialize karo
npm init -y

# Step 3: Dependencies install karo
npm install express mongoose cors dotenv
npm install -D nodemon jest

# Step 4: Scripts add karo package.json mein
# "scripts": {
#   "dev": "nodemon server.js",
#   "start": "node server.js"  
# }

# Step 5: Development start karo
npm run dev

Troubleshooting Commands

bash
# Cache clear karo - agar koi installation issue ho
npm cache clean --force

# Node modules delete karo aur phir se install karo
rm -rf node_modules
npm install

# Global packages list karo
npm list -g --depth=0

Yaad rakhne wali baatein:

  • npm install = dependencies download karta hai

  • npm run [script] = package.json ke scripts execute karta hai

  • -g flag = global installation ke liye

  • --save-dev = development dependencies ke liye

Comments