npm init # Create package.json interactively
npm init -y # Create package.json with defaults
npm install # Install all dependencies from package.json
npm install < package > # Install package locally
npm install -g < package > # Install package globally
npm install < package > # Adds to "dependencies"
npm install < package >-D # Adds to "devDependencies"
#or
npm install < package > --save-dev # Adds to "devDependencies"
npm install < package > --save-optional # Adds to "optionalDependencies"
Version Format: MAJOR.MINOR.PATCH (e.g., 1.4.2)
{
"dependencies": {
"exact": "1.2.3", // Exact version only
"patch": "~1.2.3", // >=1.2.3 <1.3.0 (patch updates)
"minor": "^1.2.3", // >=1.2.3 <2.0.0 (minor + patch, DEFAULT)
"range": ">=1.2.3 <2.1.0", // Custom range
"latest": "*", // Any version (dangerous!)
"major": "1.x", // >=1.0.0 <2.0.0
}
}
^1.2.3 - Caret (most common): Compatible minor/patch updates~1.2.3 - Tilde : Compatible patch updates only1.2.x - Wildcard : Any patch version>1.2.3 - Greater than specific versionlatest - Latest published versionPackage Management
npm outdated # Show outdated packages
npm update # Update packages (respecting semver)
npm update < package > # Update specific package
npm update -g # Update global packages
npm view < package > # Show package info
npm view < package > versions # Show all available versions
npm search < package > # Search npm registry
npm list # List installed packages (tree)
npm list --depth=0 # List top-level packages only
npm list -g --depth=0 # List global packages
npm uninstall < package > # Remove package
npm uninstall -g < package > # Remove global package
npm prune # Remove unused packages
npm login # Login to npm registry
npm publish # Publish package
npm version patch # Bump patch version (1.2.3 → 1.2.4)
npm version minor # Bump minor version (1.2.3 → 1.3.0)
npm version major # Bump major version (1.2.3 → 2.0.0)
npm version 1.4.0 # Set specific version
npm deprecate < pkg >@< ver > # Deprecate a version
npm cache clean --force # Clear npm cache
npm audit # Check for vulnerabilities
npm audit fix # Fix vulnerabilities automatically
npm doctor # Check npm environment
npm ci # Clean install (uses package-lock.json)
npm config list # Show all config
npm config get < key > # Get config value
npm config set < key > < val > # Set config value
npm config delete < key > # Delete config value
package-lock.json - Ensures consistent installsnpm ci in CI/CD pipelines instead of npm install^ for dependencies - Allows safe updatesnpm audit regularly for security.npmrc for project-specific configs