Node.js NPM: Complete Basic to Advanced Tutorial

Master package management, scripts, security, and publishing workflows

Beginner to Advanced Real-world Workflows Interview Ready

Table of Contents

  1. What is NPM?
  2. NPM Basics
  3. Package.json Deep Dive
  4. Installing Packages
  5. Managing Dependencies
  6. NPM Scripts
  7. Semantic Versioning
  8. Advanced NPM Features
  9. Creating and Publishing Packages
  10. NPM Security
  11. NPM Performance Optimization
  12. NPM Alternatives
  13. Practical Examples
  14. NPM Commands Reference

What is NPM?

NPM (Node Package Manager) is the default package manager for Node.js and the largest software registry in the world.

MetricValue
Total Packages2+ million
Weekly Downloads50+ billion
Daily New Packages1,000+
Monthly Users12+ million
Created2010 by Isaac Schlueter
NPM responsibilities:
- Package installation
- Version management
- Dependency resolution
- Script running
- Package publishing
- Registry hosting

NPM Basics

Installation & Setup

npm --version
npm install -g npm@latest
npm config list

Start a New Project

mkdir my-project
cd my-project
npm init
npm init -y

Basic Commands

npm install lodash
npm i -D nodemon jest
npm uninstall lodash
npm update
npm list --depth=0
npm outdated
npm view express

Package.json Deep Dive

package.json is the core configuration file for metadata, scripts, dependencies, compatibility, and publishing settings.

Complete Example

{
  "name": "my-awesome-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "jest"
  },
  "dependencies": { "express": "^4.18.2" },
  "devDependencies": { "nodemon": "^3.0.1", "jest": "^29.7.0" },
  "engines": { "node": ">=18.0.0", "npm": ">=9.0.0" },
  "private": true
}
FieldPurposeRequired
namePackage nameYes
versionSemver versionYes
scriptsCustom commandsNo
dependenciesProduction dependenciesNo
devDependenciesDevelopment dependenciesNo
enginesNode/npm compatibilityNo

Installing Packages

# Local
npm install express
# Global
npm install -g nodemon
# Dev dependency
npm install -D jest
# Production only
npm install --production
# Exact version
npm install -E express
# Specific version/tag
npm install express@4.18.2
npm install express@latest

Install Patterns

npm install
npm ci
npm install --dry-run
npm install --force

Managing Dependencies

Dependency Types

{
  "dependencies": {},
  "devDependencies": {},
  "peerDependencies": {},
  "optionalDependencies": {}
}

Commands

npm list --depth=0
npm outdated
npm update
npm uninstall express
npm dedupe
npm ci

NPM Scripts

{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "jest",
    "build": "webpack --mode production",
    "lint": "eslint src/"
  }
}
npm start
npm run dev
npm run test -- --coverage

Hooks

{
  "scripts": {
    "pretest": "npm run lint",
    "test": "jest",
    "posttest": "npm run coverage"
  }
}

Semantic Versioning

MAJOR.MINOR.PATCH
4.18.2
| |  +-- patch fixes
| +----- minor features (backward compatible)
+------- major breaking changes
SyntaxMeaningMatches
1.2.3ExactOnly 1.2.3
^1.2.3Compatible major1.x.x
~1.2.3Compatible minor1.2.x
1.2.xPatch wildcard1.2.0+
npm version patch
npm version minor
npm version major
npm view express version

Advanced NPM Features

Workspaces

{
  "private": true,
  "workspaces": ["packages/*"]
}
npm install --workspace=packages/api
npm run test --workspaces

Link, Cache, Config, Audit

npm link
npm cache verify
npm config list
npm audit
npm audit fix

Creating and Publishing Packages

Workflow

mkdir my-awesome-package
cd my-awesome-package
npm init
npm test
npm login
npm pack
npm publish

Version and release

npm version patch
npm publish --tag beta
npm deprecate my-package@"<1.2.0" "Security issues"

NPM Security

npm audit
npm audit fix
npm audit signatures
npm profile enable-2fa auth-and-writes

CI/CD secure npmrc

//registry.npmjs.org/:_authToken=${NPM_TOKEN}
registry=https://registry.npmjs.org/
always-auth=true

NPM Performance Optimization

npm ci
npm config set progress false
npm install --prefer-offline --no-audit --no-fund
npm dedupe
npm prune --production

Alternative manager speed comparison

ManagerInstall TimeDisk UsageLock File
npm2-5 min200-500MBpackage-lock.json
yarn1-3 min180-450MByarn.lock
pnpm1-2 min50-100MBpnpm-lock.yaml
bun30-60 sec150-400MBbun.lockb

NPM Alternatives

Yarn

npm install -g yarn
yarn install
yarn add express

pnpm

npm install -g pnpm
pnpm install
pnpm add express

Bun

curl -fsSL https://bun.sh/install | bash
bun install
bun add express

Practical Examples

1) Full setup scripts

{
  "scripts": {
    "setup": "npm install && npm run db:migrate && npm run db:seed",
    "dev": "concurrently \"npm run dev:backend\" \"npm run dev:frontend\"",
    "deploy": "npm run test && npm run build && node scripts/deploy.js"
  }
}

2) Custom deploy script args

// node scripts/deploy.js production 1.2.0
const args = process.argv.slice(2);
const environment = args[0] || 'staging';
const version = args[1] || 'latest';

3) CI pipeline steps

npm ci
npm run lint
npm run test
npm run build
npm publish

NPM Commands Reference

CategoryCommandDescription
Initnpm initCreate package.json
Installnpm install / npm ciInstall dependencies
Managenpm update / npm uninstallUpdate/remove packages
Scriptsnpm run <script>Execute scripts
Publishnpm publishPublish package
Securitynpm auditCheck vulnerabilities
Confignpm config listManage npm config
Infonpm view / npm searchPackage metadata/search

Common Options

--save / -S
--save-dev / -D
--save-exact / -E
--global / -g
--production
--no-save
--dry-run
--force
--workspace / -w

10 Interview Questions + 10 MCQs

Interview Pattern 10 Q&A
1What is the difference between npm install and npm ci?easy
Answer: npm install resolves and may update lock file; npm ci performs clean, lockfile-strict install for CI.
2Why commit package-lock.json?easy
Answer: It ensures deterministic installs across environments.
3When should a package be in devDependencies?easy
Answer: When needed only during development/testing/build (e.g., jest, eslint).
4What is peerDependencies used for?medium
Answer: To declare compatible host package versions expected in consumer project.
5How do you prevent accidental package publishing?easy
Answer: Set "private": true in package.json.
6What does npm audit do?medium
Answer: Scans dependencies for known security vulnerabilities.
7Difference between ^ and ~ in semver?medium
Answer: ^ allows compatible major range; ~ allows patch updates within same minor.
8When to use npm link?medium
Answer: While developing a local package and testing it in another local project.
9How do npm workspaces help monorepos?hard
Answer: They manage multiple packages with shared install, consistent scripts, and inter-package workflows.
10What is prepublishOnly script for?medium
Answer: Runs checks/build right before publish to ensure package quality.

10 NPM MCQs

1

Command to initialize package.json with defaults?

Anpm init --fast
Bnpm init -y
Cnpm create
Dnpm config init
Explanation: npm init -y accepts defaults.
2

Which command is ideal for CI installs?

Anpm install
Bnpm ci
Cnpm cache add
Dnpm link
Explanation: npm ci is optimized for CI with lockfile fidelity.
3

Dev dependency install flag is:

A-G
B-D
C-P
D-L
Explanation: Use -D or --save-dev.
4

What does caret (^) allow?

AOnly exact version
BPatch updates only
CCompatible major range
DAny major
Explanation: Caret permits updates within same major (usually).
5

Which field prevents publish?

A"main"
B"private": true
C"hidden": true
D"publish": false
Explanation: "private": true blocks publishing.
6

Command to check vulnerabilities?

Anpm secure
Bnpm verify
Cnpm audit
Dnpm check-risk
Explanation: npm audit checks advisories.
7

Which command runs custom script?

Anpm execute build
Bnpm run build
Cnode build
Dnpm start build
Explanation: Use npm run <script>.
8

Command to publish a package?

Anpm ship
Bnpm release
Cnpm publish
Dnpm deploy
Explanation: Publish via npm publish.
9

Which feature helps manage monorepos in npm?

Anpm bundles
Bnpm workspaces
Cnpm chains
Dnpm clusters
Explanation: npm workspaces support monorepo package management.
10

Which command checks outdated dependencies?

Anpm old
Bnpm stale
Cnpm update --check
Dnpm outdated
Explanation: npm outdated reports current/wanted/latest versions.