Master the most important npm commands for initializing projects, installing packages, and managing dependencies.
Create package.json
npm init -y
Copied!
Add dependencies
npm install express
Copied!
Installs package and adds to dependencies
From package.json
npm install
or
npm i
Copied!
Reads package.json and installs all listed dependencies in node_modules
Development only
npm install -D nodemon
Copied!
Installs packages only needed during development (testing, linting, etc.)
-D or --save-devSystem-wide access
npm install -g nodemon
Copied!
Installs package globally on your system, accessible from any directory
Clean up dependencies
npm uninstall express
Copied!
Keep dependencies fresh
npm update
// Update all
Copied!
npm update express
// Update specific
Copied!
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
The heart of your Node.js project. Learn every field and its purpose.
{ "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" } }
Project identifier
Semantic versioning
Entry point
Automation commands
Environment requirements
Discovery & source
Understanding the difference is crucial for production optimization.
Production Required
Packages essential for your application to run in production. These are installed when you deploy your app.
npm install express
Copied!
Development Only
Packages only needed during development and testing. Not installed in production (when NODE_ENV=production).
npm install -D nodemon
Copied!
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!
Automate your workflow with custom commands. Special scripts vs custom scripts explained.
Launch production application
"start": "node app.js"
npm start
No "run" needed
Copied!
Execute test suite
"test": "jest"
npm test
No "run" needed
Copied!
Development with auto-reload
"dev": "nodemon app.js"
npm run dev
Requires "run"
Copied!
npm start
Runs start script
npm test
Runs test script
npm stop
Runs stop script (if defined)
npm restart
Runs restart script
npm run dev
Runs dev script
npm run lint
Runs lint script
npm run build
Runs build script
Understanding version numbers and symbols in package.json
Example: 4.18.2
Incompatible API changes. Requires code updates.
Backward-compatible functionality additions.
Backward-compatible bug fixes. Safe to update.
Compatible with version
Allows updates that do not change the leftmost non-zero digit. Allows minor and patch updates.
Approximately equivalent
Allows only patch updates. Use when you want to minimize changes and only accept bug fixes.
Specific version only
Installs exactly this version. No updates allowed. Use for maximum stability.
Always newest
Installs the latest version available. Use with caution as it may introduce breaking changes.