What is a Package Manager?
npm, yarn, pip, cargo - package managers download and manage code libraries for you. The app store for code.
npm install axios and the package manager handles everything - downloading the code, tracking the version, and managing updates. npm (JavaScript), pip (Python), and cargo (Rust) are the most common ones.What package managers do
Without a package manager, to use someone else’s code you’d have to:
- Find the code
- Download it
- Put it in the right place
- Update it manually when new versions come out
- Track what version you’re using
- Repeat for every library
Package managers automate all of this:
npm install axios # Downloads axios, tracks version in package.json
pip install flask # Downloads Flask and its dependencies
cargo add serde # Downloads serde crateThe main package managers
JavaScript/Node.js
npm (Node Package Manager)
- Comes with Node.js
- Most widely used
- Registry: npmjs.com
npm install package-name # Install a package
npm install # Install all packages from package.json
npm uninstall package-name # Remove a package
npm update # Update packages
npm run dev # Run scripts defined in package.jsonyarn
- Alternative to npm
- Slightly faster
- Same registry as npm
yarn add package-name
yarn # Same as npm install
yarn remove package-namepnpm
- Faster and uses less disk space
- Growing in popularity
pnpm install package-name
pnpm installPython
pip
- Comes with Python
- Registry: pypi.org
pip install package-name
pip install -r requirements.txt # Install from file
pip uninstall package-name
pip freeze > requirements.txt # Save installed packagespoetry / pipenv
- More modern Python package managers
- Better dependency resolution
- Built-in virtual environment management
Others
| Language | Package Manager | Registry |
|---|---|---|
| Rust | cargo | crates.io |
| Go | go mod | proxy.golang.org |
| Ruby | gem / bundler | rubygems.org |
| PHP | composer | packagist.org |
| Java | maven / gradle | Maven Central |
Key concepts
package.json (JavaScript)
This file defines your project and its dependencies:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"dev": "next dev",
"build": "next build"
},
"dependencies": {
"react": "^18.2.0",
"axios": "^1.6.0"
},
"devDependencies": {
"typescript": "^5.0.0"
}
}- dependencies: Packages needed to run your app
- devDependencies: Packages only needed during development (testing, building)
- scripts: Commands you can run with
npm run <script-name>
Lock files
Lock files record the exact versions installed:
| Manager | Lock file |
|---|---|
| npm | package-lock.json |
| yarn | yarn.lock |
| pnpm | pnpm-lock.yaml |
| pip | requirements.txt or Pipfile.lock |
Always commit lock files to Git. They ensure everyone gets identical versions.
Version numbers
Packages use semantic versioning: MAJOR.MINOR.PATCH (e.g., 2.3.1)
- MAJOR: Breaking changes (2.x → 3.x might break your code)
- MINOR: New features, backwards compatible (2.3 → 2.4)
- PATCH: Bug fixes (2.3.0 → 2.3.1)
In package.json, prefixes control what gets installed:
| Prefix | Meaning | Example |
|---|---|---|
^ | Compatible updates | ^2.3.0 installs 2.x.x (not 3.0.0) |
~ | Patch updates only | ~2.3.0 installs 2.3.x (not 2.4.0) |
| none | Exact version | 2.3.0 installs exactly 2.3.0 |
node_modules
When you run npm install, packages go into the node_modules folder. This folder:
- Can be huge (hundreds of MB)
- Contains thousands of files
- Should NOT be committed to Git
- Can be regenerated anytime from package.json + lock file
Add to .gitignore:
node_modules/Common workflows
Starting a new project
# JavaScript
npm init -y # Creates package.json
npm install react next # Install packages
# Python
python -m venv venv # Create virtual environment
source venv/bin/activate # Activate it (Mac/Linux)
pip install flask # Install packages
pip freeze > requirements.txtCloning an existing project
git clone https://github.com/user/repo.git
cd repo
# JavaScript
npm install # Installs everything from package.json
# Python
pip install -r requirements.txtAdding a package
# JavaScript (production dependency)
npm install axios
# JavaScript (dev dependency)
npm install -D typescript
# Python
pip install requestsUpdating packages
# See what's outdated
npm outdated
# Update all
npm update
# Update a specific package
npm install axios@latestRemoving a package
npm uninstall axios
pip uninstall requestsCommon problems
“npm install” fails
# Delete and reinstall
rm -rf node_modules package-lock.json
npm install
# If it's a permissions issue (Mac/Linux)
sudo chown -R $(whoami) ~/.npmWrong Node version
Some projects need specific Node versions. Use nvm (Node Version Manager):
# Install nvm, then:
nvm install 18
nvm use 18Conflicting dependencies
npm install --legacy-peer-deps
# or
npm install --forcePython virtual environment issues
Always use virtual environments to avoid conflicts:
# Create
python -m venv venv
# Activate (Mac/Linux)
source venv/bin/activate
# Activate (Windows)
.\venv\Scripts\activate
# Now pip installs go to this environment only
pip install flaskThe registry
Packages live in registries:
- npmjs.com: Browse JavaScript packages
- pypi.org: Browse Python packages
- crates.io: Browse Rust packages
Before installing a package, check:
- Download count (popular = probably safe)
- Last updated (actively maintained?)
- GitHub stars and issues
- Security advisories
Package manager commands cheat sheet
| Task | npm | yarn | pip |
|---|---|---|---|
| Install all | npm install | yarn | pip install -r requirements.txt |
| Add package | npm install pkg | yarn add pkg | pip install pkg |
| Add dev package | npm install -D pkg | yarn add -D pkg | (no distinction) |
| Remove | npm uninstall pkg | yarn remove pkg | pip uninstall pkg |
| Update all | npm update | yarn upgrade | pip install --upgrade -r requirements.txt |
| Run script | npm run dev | yarn dev | N/A |
| List installed | npm list | yarn list | pip list |
| Check outdated | npm outdated | yarn outdated | pip list --outdated |
Further reading
- What is a dependency? : Understanding what gets installed
- Common error messages explained : When npm fails
- What is an environment variable? : Configuration alongside packages
Frequently asked questions
Should I use npm or yarn?
Why are there so many different package managers?
Is it safe to install packages?
npm audit to check for known vulnerabilities.