NPM and package.json
What is NPM?
NPM (Node Package Manager) is the default package manager for Node.js. It’s the world’s largest software registry with over 2 million packages.
NPM Commands
Installation
# Install package locally
npm install express
# Install globally
npm install -g nodemon
# Install as dev dependency
npm install --save-dev jest
# Install specific version
npm install express@4.18.0
# Install all dependencies
npm installPackage Management
# Update packages
npm update
# Remove package
npm uninstall express
# List installed packages
npm list
# Check outdated packages
npm outdated
# Audit for vulnerabilities
npm audit
npm audit fixScripts
# Run script
npm start
npm test
npm run dev
# Run with arguments
npm run build -- --productionpackage.json
The package.json file contains metadata about your project and its dependencies.
Basic Structure
{
"name": "my-app",
"version": "1.0.0",
"description": "My Node.js application",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "jest",
"build": "webpack --mode production"
},
"keywords": ["nodejs", "express"],
"author": "Your Name",
"license": "MIT",
"dependencies": {
"express": "^4.18.0",
"mongoose": "^6.0.0"
},
"devDependencies": {
"nodemon": "^2.0.0",
"jest": "^29.0.0"
}
}Key Fields
name
{
"name": "my-package"
}version
{
"version": "1.0.0"
}scripts
{
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest --coverage",
"lint": "eslint .",
"build": "webpack"
}
}dependencies
{
"dependencies": {
"express": "^4.18.0",
"dotenv": "^16.0.0"
}
}devDependencies
{
"devDependencies": {
"nodemon": "^2.0.0",
"jest": "^29.0.0",
"eslint": "^8.0.0"
}
}Semantic Versioning
MAJOR.MINOR.PATCH
^ ^ ^
| | |
| | └─ Bug fixes
| └─────── New features (backward compatible)
└───────────── Breaking changesVersion Ranges
{
"dependencies": {
"express": "4.18.0", // Exact version
"mongoose": "^6.0.0", // Compatible (6.x.x)
"lodash": "~4.17.0", // Approximately (4.17.x)
"axios": "*", // Any version (not recommended)
"moment": ">=2.29.0", // Greater than or equal
"react": "18.x" // Any 18.x version
}
}package-lock.json
Locks exact versions of dependencies for consistent installs.
{
"name": "my-app",
"version": "1.0.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"node_modules/express": {
"version": "4.18.2",
"resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz",
"integrity": "sha512-..."
}
}
}NPM Scripts
Common Scripts
{
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest",
"test:watch": "jest --watch",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"build": "webpack --mode production",
"clean": "rm -rf dist",
"prebuild": "npm run clean",
"postbuild": "echo 'Build complete'"
}
}Pre and Post Hooks
{
"scripts": {
"pretest": "npm run lint",
"test": "jest",
"posttest": "npm run coverage"
}
}.npmrc Configuration
# .npmrc
registry=https://registry.npmjs.org/
save-exact=true
engine-strict=trueCreating a Package
Initialize
npm init
npm init -y # Skip questionsPublish
# Login
npm login
# Publish
npm publish
# Publish with tag
npm publish --tag betaNPM vs Yarn vs PNPM
| Feature | NPM | Yarn | PNPM |
|---|---|---|---|
| Speed | Good | Fast | Fastest |
| Disk Space | More | More | Less |
| Lock File | package-lock.json | yarn.lock | pnpm-lock.yaml |
| Workspaces | Yes | Yes | Yes |
Best Practices
- Use package-lock.json for consistent installs
- Specify exact versions for critical dependencies
- Audit regularly for security vulnerabilities
- Use .npmignore to exclude files from package
- Keep dependencies updated but test thoroughly
- Use semantic versioning properly
Interview Tips
- Explain NPM purpose: Package manager for Node.js
- Describe package.json: Project metadata and dependencies
- Show semantic versioning: MAJOR.MINOR.PATCH
- Discuss version ranges: ^, ~, exact versions
- Mention package-lock.json: Locks dependency versions
- Show common commands: install, update, audit
- Explain scripts: Custom commands in package.json
Summary
NPM is Node.js’s package manager for installing and managing dependencies. package.json defines project metadata, dependencies, and scripts. package-lock.json ensures consistent installs. Use semantic versioning for dependency management and npm scripts for automation.
Test Your Knowledge
Take a quick quiz to test your understanding of this topic.