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:

SymbolMeaningExampleAllows
^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 version1.2.31.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:

  1. Updates version in package.json
  2. Creates git commit
  3. Creates git tag

Troubleshooting Commands

Clean Installation

CI Install

npm ci vs npm install:

  • npm ci is faster
  • npm ci requires package-lock.json
  • npm ci deletes node_modules before install
  • npm ci won’t update package-lock.json
  • Use npm ci in 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

CommandDescription
npm installInstall all dependencies
npm install pkgInstall package
npm install -D pkgInstall dev dependency
npm uninstall pkgRemove package
npm updateUpdate packages
npm outdatedCheck outdated packages
npm run scriptRun script from package.json
npm testRun tests
npm startStart application
npm auditSecurity audit
npm audit fixFix vulnerabilities
npm cache clean --forceClear cache
npm ciClean install (CI)
npm listList installed packages
npm view pkgShow package info

Common Issues

EACCES Permission Error

Package Lock Conflicts

Peer Dependency Warnings

Best Practices

Do

  • Commit package-lock.json to version control
  • Use npm ci in CI/CD pipelines
  • Run npm audit regularly
  • Keep dependencies updated
  • Use exact versions for critical packages
  • Use .npmrc for 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