npm Overview
npm is the world's largest software registry and package manager for JavaScript. It consists of a command line client and an online database of public and paid-for private packages, called the npm registry.
Key Features
- Package Management: Install, share, and distribute code.
- Dependency Management: Track and manage project dependencies.
- Script Running: Define and run project scripts.
- Version Control: Semantic versioning support.
- Registry Access: Access to over 1.3 million packages.
- Workspaces: Manage multiple packages in a single repository.
- Security: Vulnerability scanning with npm audit.
Installation & Setup
Installation Steps:
- Install Node.js which includes npm from official website
- Verify installation with:
npm -v - Initialize a new project with:
npm init - Configure your registry if needed (default is https://registry.npmjs.org/)
Common Commands:
npm install
Install a package and its dependencies.
npm run
Run a script defined in package.json.
npm update
Update packages to their latest version.
npm publish
Publish a package to the registry.
Productivity Tips
npx
Use npx to run packages without installing them globally.
package-lock.json
Commit this file to ensure consistent installs across environments.
npm ci
Use for faster, cleaner installs in CI/CD environments.
npm scripts
Define custom scripts in package.json for common tasks.