📚 3 min read
npm Guide ​
npm (Node Package Manager) is the default package manager for Node.js and the world's largest software registry. It's installed automatically with Node.js and provides a robust foundation for managing JavaScript packages.
Installation ​
npm comes bundled with Node.js:
bash
# Check if npm is installed
npm -v
# Update npm to latest version
npm install -g npm@latestKey Commands ​
Project Initialization ​
bash
# Create a new package.json
npm init
# Create with defaults
npm init -yPackage Installation ​
bash
# Install all dependencies
npm install
# Install a package
npm install package-name
# Install as dev dependency
npm install --save-dev package-name
# Install globally
npm install -g package-name
# Install specific version
npm install package-name@versionPackage Management ​
bash
# Update packages
npm update
# Remove package
npm uninstall package-name
# List installed packages
npm list
# List outdated packages
npm outdated
# Run security audit
npm audit
# Fix security issues
npm audit fixScripts ​
bash
# Run a script defined in package.json
npm run script-name
# Common built-in scripts
npm start
npm test
npm buildConfiguration ​
npm Configuration File (.npmrc) ​
ini
# Set default registry
registry=https://registry.npmjs.org/
# Set authentication token
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
# Set default save prefix
save-prefix=~
# Enable package-lock
package-lock=truepackage.json ​
json
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"test": "jest",
"build": "webpack"
},
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^27.0.0"
}
}Workspaces ​
npm supports workspaces for monorepo management:
json
{
"name": "my-monorepo",
"workspaces": ["packages/*"]
}Workspace Commands ​
bash
# Install dependencies for all workspaces
npm install
# Run command in specific workspace
npm run test --workspace=package-name
# Run command in all workspaces
npm run test --workspacesBest Practices ​
Version Control
- Always commit
package.jsonandpackage-lock.json - Use
.npmignoreto exclude unnecessary files - Use
save-exactfor critical dependencies
- Always commit
Security
- Regularly run
npm audit - Use
npm ciin CI/CD pipelines - Keep npm and Node.js updated
- Use official registry or trusted sources
- Regularly run
Performance
- Use
npm cifor clean installs - Leverage caching in CI/CD
- Use
--productionflag in production - Clean cache periodically with
npm cache clean
- Use
Dependency Management
- Review dependencies regularly
- Use
npm outdatedto check updates - Consider using
npm-checkfor updates - Be cautious with
npm update
Common Issues and Solutions ​
EACCES Permission Errors ​
bash
# Fix permissions globally
sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/local/lib/node_modulesPackage Lock Conflicts ​
bash
# Regenerate package-lock.json
rm package-lock.json
npm installCache Issues ​
bash
# Clear npm cache
npm cache clean --force
# Verify cache
npm cache verifyAdvanced Features ​
npm Scripts with Arguments ​
json
{
"scripts": {
"start": "node server.js",
"start:dev": "NODE_ENV=development npm run start",
"start:prod": "NODE_ENV=production npm run start"
}
}Custom Registry Configuration ​
bash
# Set custom registry
npm config set registry https://custom-registry.com
# Use registry for specific scope
npm config set @myorg:registry https://custom-registry.comPublishing Packages ​
bash
# Login to npm
npm login
# Publish package
npm publish
# Publish scoped package
npm publish --access publicMore content coming soon...