Complete Reference Guide

Master Node.js & npm

Your comprehensive guide to package management, dependency handling, and project configuration. From beginner basics to advanced workflows.

20+
Essential Commands
100%
Interactive Code
5
Core Sections
Possibilities

Essential Commands

Master the most important npm commands for initializing projects, installing packages, and managing dependencies.

Initialize Project

Create package.json

Essential
npm init -y Copied!
  • Creates package.json with defaults
  • Sets up project metadata
  • Prepares for dependency management

Install Package

Add dependencies

npm install express Copied!

Installs package and adds to dependencies

express mongoose axios

Install All Dependencies

From package.json

npm install or npm i Copied!

Reads package.json and installs all listed dependencies in node_modules

Dev Dependencies

Development only

Dev Only
npm install -D nodemon Copied!

Installs packages only needed during development (testing, linting, etc.)

Short flag: -D or --save-dev

Global Install

System-wide access

npm install -g nodemon Copied!

Installs package globally on your system, accessible from any directory

Remove Package

Clean up dependencies

npm uninstall express Copied!
npm remove npm rm npm un

Update Packages

Keep dependencies fresh

npm update // Update all Copied!
npm update express // Update specific Copied!

List & Audit

Inspect & secure

npm list --depth=0 Copied!
npm audit Copied!
npm audit fix Copied!
npm outdated

Check for updates

npm search pkg

Search registry

npm view pkg

Package info

npm ls

List all packages

Understanding package.json

The heart of your Node.js project. Learn every field and its purpose.

package.json Structure

package.json
{
  "name": "my-awesome-app",
  "version": "1.0.0",
  "description": "A fantastic Node.js application",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.20"
  },
  "keywords": ["nodejs", "api"],
  "author": "Your Name",
  "license": "MIT",
  "engines": {
    "node": ">=14.0.0"
  }
}
Copied to clipboard!
01

name

Project identifier

02

version

Semantic versioning

03

main

Entry point

04

scripts

Automation commands

05

engines

Environment requirements

06

keywords & repository

Discovery & source

Dependencies vs DevDependencies

Understanding the difference is crucial for production optimization.

dependencies

Production Required

Packages essential for your application to run in production. These are installed when you deploy your app.

express
Web server framework
mongoose
MongoDB ODM
axios
HTTP client
bcrypt
Password hashing
dotenv
Environment variables
npm install express Copied!

devDependencies

Development Only

Packages only needed during development and testing. Not installed in production (when NODE_ENV=production).

nodemon
Auto-restart server
jest
Testing framework
eslint
Code linting
prettier
Code formatting
webpack
Module bundler
npm install -D nodemon Copied!

Production Optimization Tip

When deploying to production, use npm ci --only=production to install only production dependencies, significantly reducing node_modules size and deployment time.

npm ci --only=production Copied!

NPM Scripts

Automate your workflow with custom commands. Special scripts vs custom scripts explained.

start

Launch production application

"start": "node app.js"
npm start No "run" needed Copied!

test

Execute test suite

"test": "jest"
npm test No "run" needed Copied!

dev

Development with auto-reload

"dev": "nodemon app.js"
npm run dev Requires "run" Copied!

Script Execution Rules

Special Scripts (No "run" needed)

  • npm start Runs start script
  • npm test Runs test script
  • npm stop Runs stop script (if defined)
  • npm restart Runs restart script

Custom Scripts (Requires "run")

  • npm run dev Runs dev script
  • npm run lint Runs lint script
  • npm run build Runs build script
  • All custom scripts require "run" keyword

Semantic Versioning

Understanding version numbers and symbols in package.json

Version Format: MAJOR.MINOR.PATCH

Example: 4.18.2

MAJOR
Breaking Changes

Incompatible API changes. Requires code updates.

X.0.0
MINOR
New Features

Backward-compatible functionality additions.

0.X.0
PATCH
Bug Fixes

Backward-compatible bug fixes. Safe to update.

0.0.X
^

Caret (^)

Compatible with version

Allows updates that do not change the leftmost non-zero digit. Allows minor and patch updates.

"express": "^4.18.2"
4.19.0 ✓ (Minor update)
4.18.5 ✓ (Patch update)
5.0.0 ✗ (Major change)
~

Tilde (~)

Approximately equivalent

Allows only patch updates. Use when you want to minimize changes and only accept bug fixes.

"express": "~4.18.2"
4.18.5 ✓ (Patch update)
4.19.0 ✗ (Minor change)
5.0.0 ✗ (Major change)
1.2.3

Exact

Specific version only

Installs exactly this version. No updates allowed. Use for maximum stability.

"express": "4.18.2"
4.18.2 ✓ (Exact match)
4.18.3 ✗ (Any change)
*

Latest (*)

Always newest

Installs the latest version available. Use with caution as it may introduce breaking changes.

"express": "*" or "latest"
Latest ✓ (Always newest)
Risky for production use