npm & Package Management
npm (Node Package Manager) is the world's largest software registry. It manages project dependencies, scripts, and versioning through the package.json configuration file.
1 Essential npm Commands
Terminal — npm Commands
# Initialize a new project (creates package.json)
npm init -y
# Install a production dependency
npm install express
# Install a dev dependency (only needed during development)
npm install --save-dev nodemon jest
# Install packages globally
npm install -g typescript
# Update all packages to latest compatible versions
npm update
# Remove a package
npm uninstall lodash
# List installed packages (top level)
npm list --depth=0
# Audit for security vulnerabilities
npm audit
npm audit fix
# Run a script defined in package.json
npm run dev
npm test
2 package.json Deep Dive
JSON — package.json
{
"name": "my-node-api",
"version": "1.0.0",
"description": "A RESTful API built with Node.js",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"test": "jest --coverage",
"lint": "eslint src/"
},
"dependencies": {
"express": "^4.18.2",
"mongoose": "^8.1.0",
"dotenv": "^16.4.1"
},
"devDependencies": {
"nodemon": "^3.0.2",
"jest": "^29.7.0",
"eslint": "^8.56.0"
},
"engines": {
"node": ">=18.0.0"
}
}
3 Code Challenge
Challenge: Initialize a new Node.js project, install
lodash as a dependency, and write a script that uses _.groupBy() to group an array of user objects by their role property.