npm Commands Reference
Quick reference for npm package management in EBRAINS projects.
Installation Commands
Install Dependencies
What it does:
- Installs all packages listed in
package.json - Creates
node_modules/directory - Generates/updates
package-lock.json - Respects version ranges in package.json
Install Specific Package
Install Global Package
Install from Different Sources
Package Management
Update Packages
Uninstall Packages
List Packages
Scripts
Run Scripts
Special Scripts
Script Examples
package.json:
Run them:
Version Management
Check Versions
Semantic Versioning in package.json
Version range symbols:
| Symbol | Meaning | Example | Allows |
|---|---|---|---|
^ | Compatible versions | ^1.2.3 | >=1.2.3 <2.0.0 |
~ | Patch updates | ~1.2.3 | >=1.2.3 <1.3.0 |
> | Greater than | >1.2.3 | >1.2.3 |
>= | Greater or equal | >=1.2.3 | >=1.2.3 |
< | Less than | <1.2.3 | <1.2.3 |
<= | Less or equal | <=1.2.3 | <=1.2.3 |
* | Any version | * | Any |
| (none) | Exact version | 1.2.3 | 1.2.3 only |
Configuration
Set Configuration
View Configuration
Configuration Files
Locations:
- User config:
~/.npmrc - Project config:
<project>/.npmrc - Global config:
/usr/local/etc/npmrc
Example .npmrc:
Cache Management
Cache Commands
When to clear cache:
- Strange installation errors
- Corrupted packages
- Disk space issues
Package Information
View Package Info
Search Packages
Security
Audit Packages
Package Integrity
Publishing (For Library Maintainers)
Publish Package
Version Bumping
What npm version does:
- Updates version in package.json
- Creates git commit
- Creates git tag
Troubleshooting Commands
Clean Installation
CI Install
npm ci vs npm install:
npm ciis fasternpm cirequires package-lock.jsonnpm cideletes node_modules before installnpm ciwon’t update package-lock.json- Use
npm ciin CI/CD pipelines
Fix Permission Issues
Rebuild Native Modules
Common Workflows
Starting New Project
Update Project Dependencies
Troubleshoot Installation Issues
Environment Variables
Use in Scripts
package.json:
Access in code:
Quick Reference Table
| Command | Description |
|---|---|
npm install | Install all dependencies |
npm install pkg | Install package |
npm install -D pkg | Install dev dependency |
npm uninstall pkg | Remove package |
npm update | Update packages |
npm outdated | Check outdated packages |
npm run script | Run script from package.json |
npm test | Run tests |
npm start | Start application |
npm audit | Security audit |
npm audit fix | Fix vulnerabilities |
npm cache clean --force | Clear cache |
npm ci | Clean install (CI) |
npm list | List installed packages |
npm view pkg | Show package info |
Common Issues
EACCES Permission Error
Package Lock Conflicts
Peer Dependency Warnings
Best Practices
Do
- Commit
package-lock.jsonto version control - Use
npm ciin CI/CD pipelines - Run
npm auditregularly - Keep dependencies updated
- Use exact versions for critical packages
- Use
.npmrcfor project-specific config - Document scripts in README
Don’t
- Don’t commit
node_modules/ - Don’t use
sudo npm install(fix permissions instead) - Don’t ignore security warnings
- Don’t use outdated npm version
- Don’t manually edit
package-lock.json - Don’t install everything globally
- Don’t skip
npm audit fix
Related Documentation
- Nx Commands Reference - Monorepo commands
- Build Your First App Tutorial - Application development
- npm Documentation - Official npm docs
- package.json Documentation - package.json reference