VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / An Introduction to Package ManagersAn Introduction to Package Managers
VK

An Introduction to Package ManagersAn Introduction to Package Managers

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: An Introduction to Package Managers.Ensiklopedia VibeKoding: An Introduction to Package Managers.

> ๐Ÿ’ก Learning Guide: You don't have to reinvent the wheel when writing code โ€” 99% of the functionality you need has already been written and published online by someone else. A package manager is the tool that helps you find, download, and manage these "ready-made parts." This chapter revolves around one core question: how to make code dependencies reproducible, collaborative, and maintainable?> ๐Ÿ’ก Learning Guide: You don't have to reinvent the wheel when writing code โ€” 99% of the functionality you need has already been written and published online by someone else. A package manager is the tool that helps you find, download, and manage these "ready-made parts." This chapter revolves around one core question: how to make code dependencies reproducible, collaborative, and maintainable?

1. Package Managers Across Language/System Ecosystems1. Package Managers Across Language/System Ecosystems

Different programming languages and operating systems have their own ecosystem toolchains, but the underlying logic is exactly the same.Different programming languages and operating systems have their own ecosystem toolchains, but the underlying logic is exactly the same.

๐Ÿ‘‡ Try it out: Select an ecosystem you're familiar with and explore its mainstream package management tools.๐Ÿ‘‡ Try it out: Select an ecosystem you're familiar with and explore its mainstream package management tools.

1.1 Placement of Packagesing Come From โ€” The Registry1.1 Placement of Packagesing Come From โ€” The Registry

Every ecosystem has a central repository behind it that stores all downloadable packages:Every ecosystem has a central repository behind it that stores all downloadable packages:

EcosystemRegistryPackage Count
JavaScript[npmjs.com](https://npmjs.com)2 million+
Python[pypi.org](https://pypi.org)500K+
Rust[crates.io](https://crates.io)150K+
Go[pkg.go.dev](https://pkg.go.dev)500K+
macOS/Linux Tools[formulae.brew.sh](https://formulae.brew.sh)7,000+
Windows Software[winget.run](https://winget.run) / [chocolatey.org](https://chocolatey.org)Tens of thousands

1.2 JavaScript's Big Three: npm vs yarn vs pnpm1.2 JavaScript's Big Three: npm vs yarn vs pnpm

Similar functionality, with differences mainly in speed and disk usage:Similar functionality, with differences mainly in speed and disk usage:

text
Disk usage: pnpm (hard link sharing) < yarn PnP (zero node_modules) < npm (full copy) Install speed: pnpm โ‰ˆ yarn > npm Usage: npm (most universal) > pnpm (recommended for new projects) > yarn (some teams)

Recommendation: Use pnpm for new projects, stick with the existing tool for existing projects, and don't switch casually.Recommendation: Use pnpm for new projects, stick with the existing tool for existing projects, and don't switch casually.

1.3 Windows's Big Three: winget vs Chocolatey vs Scoop1.3 Windows's Big Three: winget vs Chocolatey vs Scoop

wingetChocolateyScoop
Official backingMicrosoft officialThird-partyThird-party
Requires adminPartially neededYesNot needed
Best forDaily software installationEnterprise batch deploymentDev tool management
Package countMany, growing fastMost (10,000+)Focused on dev tools

Recommendation: Use winget for daily use, scoop for dev tools, Chocolatey for enterprise automation.Recommendation: Use winget for daily use, scoop for dev tools, Chocolatey for enterprise automation.

------

2. Installing Packages โ€” What Happens Behind the Scenes2. Installing Packages โ€” What Happens Behind the Scenes

After typing npm install axios, the command line goes quiet for a few seconds, and then it's done. What exactly happened during those seconds?After typing npm install axios, the command line goes quiet for a few seconds, and then it's done. What exactly happened during those seconds?

๐Ÿ‘‡ Try it out: Select a package, click "Run," and observe the full installation process.๐Ÿ‘‡ Try it out: Select a package, click "Run," and observe the full installation process.

2.1 The Four Stages Explained2.1 The Four Stages Explained

โ‘  Dependency Resolutionโ‘  Dependency Resolution

The package manager first "understands" what you want to install. Take axios as an example โ€” it itself depends on packages like follow-redirects, form-data, etc., which also need to be installed. This process is called building the dependency tree.The package manager first "understands" what you want to install. Take axios as an example โ€” it itself depends on packages like follow-redirects, form-data, etc., which also need to be installed. This process is called building the dependency tree.

โ‘ก Fetchโ‘ก Fetch

Download all needed packages from the Registry (compressed .tgz archives). Smart package managers will:Download all needed packages from the Registry (compressed .tgz archives). Smart package managers will:

โ‘ข Linkโ‘ข Link

Extract the downloaded packages into the node_modules/ directory and set up the reference relationships.Extract the downloaded packages into the node_modules/ directory and set up the reference relationships.

โ‘ฃ Write Lockfileโ‘ฃ Write Lockfile

Write the exact version numbers from this installation into package-lock.json (or yarn.lock / pnpm-lock.yaml).Write the exact version numbers from this installation into package-lock.json (or yarn.lock / pnpm-lock.yaml).

2.2 Most Common Commands Cheat Sheet2.2 Most Common Commands Cheat Sheet

bash
# โ”€โ”€ JavaScript (npm) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ npm install # Install all dependencies per package.json npm install axios # Install a new package (production dependency) npm install -D jest # Install a dev dependency (only used during development) npm install -g tsx # Global install (available in any directory) npm uninstall axios # Uninstall a package npm update # Upgrade all packages to latest compatible versions npm run build # Run scripts defined in package.json npx create-react-app . # Run temporarily without installing to project # โ”€โ”€ Python (pip) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ pip install requests # Install a package pip install requests==2.28.0 # Install a specific version pip freeze > requirements.txt # Export current dependency list pip install -r requirements.txt # Install from a list # โ”€โ”€ Rust (cargo) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ cargo add serde # Add a dependency (auto-updates Cargo.toml) cargo build # Build the project cargo test # Run tests cargo run # Run the project # โ”€โ”€ Go (go mod) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ go get github.com/gin-gonic/gin # Add a dependency go mod tidy # Clean up dependencies (remove extras, add missing) go build ./... # Build # โ”€โ”€ Windows (winget) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ winget install Git.Git # Install software winget upgrade --all # Update all installed software

2.3 Overview of npm scripts2.3 Overview of npm scripts

The scripts field in package.json is npm's built-in task runner:The scripts field in package.json is npm's built-in task runner:

json
{ "scripts": { "dev": "vite", "build": "vite build", "test": "jest", "lint": "eslint src/" } }

Running: npm run dev, npm run build. Benefits:Running: npm run dev, npm run build. Benefits:

------

3. Global vs Local Installation3. Global vs Local Installation

This is one of the most confusing concepts for beginners.This is one of the most confusing concepts for beginners.

3.1 The Difference3.1 The Difference

bash
npm install axios # Local install: into ./node_modules/, only available in current project npm install -g typescript # Global install: into system directory, available in any project/directory
Local InstallGlobal Install
Location./node_modules/System-level directory (e.g., /usr/local/lib/)
Best forProject dependency libraries (axios, vue, react)CLI tools (tsc, eslint, create-react-app)
Version isolationEach project has independent versions โœ…One version shared across the machine โš ๏ธ
Team consistencyLockfile ensures consistency โœ…Different people may have different versions โš ๏ธ

3.2 The Golden Rule3.2 The Golden Rule

> Library dependencies (axios, lodash, vue) should always be installed locally;> Library dependencies (axios, lodash, vue) should always be installed locally;

> CLI tools (tsc, eslint) should preferably be installed locally too, invoked with npx.> CLI tools (tsc, eslint) should preferably be installed locally too, invoked with npx.

Why are CLI tools also recommended for local installation?Why are CLI tools also recommended for local installation?

Suppose you globally installed eslint@8, but Project A needs the new rules from eslint@9 โ€” you'd have to switch back and forth between global and project versions. Install eslint locally and invoke it with npx eslint ., so each project can independently configure its own version.Suppose you globally installed eslint@8, but Project A needs the new rules from eslint@9 โ€” you'd have to switch back and forth between global and project versions. Install eslint locally and invoke it with npx eslint ., so each project can independently configure its own version.

3.3 npx โ€” Run Temporarily, Don't Pollute the Environment3.3 npx โ€” Run Temporarily, Don't Pollute the Environment

npx is npm's built-in package runner that lets you run a package without installing it:npx is npm's built-in package runner that lets you run a package without installing it:

bash
# Run create-vue without installing it, to initialize a project npx create-vue my-project # Run prettier without installing it, to format files npx prettier --write src/ # Force a specific version (ignoring any installed version) npx typescript@5.4 tsc --version

Python's uvx and Rust's cargo run also provide similar "run temporarily" capabilities:Python's uvx and Rust's cargo run also provide similar "run temporarily" capabilities:

bash
uvx ruff check . # Python: temporarily run the ruff checker cargo install ripgrep # Rust: install globally, becomes system command rg

------

4. The Secret of Version Numbers โ€” Semantic Versioning4. The Secret of Version Numbers โ€” Semantic Versioning

In package.json, you'll see entries like:In package.json, you'll see entries like:

json
{ "dependencies": { "axios": "^1.6.8", "typescript": "~5.4.0" } }

What do ^ and ~ mean here?What do ^ and ~ mean here?

๐Ÿ‘‡ Try it out: Hover over the parts of a version number to understand their meaning; click range operators to see which versions are accepted.๐Ÿ‘‡ Try it out: Hover over the parts of a version number to understand their meaning; click range operators to see which versions are accepted.

4.1 Motivation for Noting Pin the Version4.1 Motivation for Noting Pin the Version

ApproachProsCons
"axios": "1.6.8" (exact pin)Fully predictableSecurity patches can't auto-update
"axios": "^1.6.8" (compatible range, recommended)Auto-get bug fixes and new featuresRarely may introduce minor incompatibilities
"axios": "*" (any version)Always latestMajor version upgrades can completely break code

Best practice: Declare ranges with ^ + pin actual versions with a lockfile โ€” use both together.Best practice: Declare ranges with ^ + pin actual versions with a lockfile โ€” use both together.

4.2 Overview of Dependency Hell4.2 Overview of Dependency Hell

When you depend on 50 packages, and each of those depends on several more, the "dependency tree" can have hundreds of nodes. If two of your dependencies need incompatible versions of the same library, you have a "dependency conflict."When you depend on 50 packages, and each of those depends on several more, the "dependency tree" can have hundreds of nodes. If two of your dependencies need incompatible versions of the same library, you have a "dependency conflict."

How different ecosystems solve this:How different ecosystems solve this:

------

5. Lockfiles โ€” The Cornerstone of Team Collaboration5. Lockfiles โ€” The Cornerstone of Team Collaboration

5.1 Motivation for needing a Lockfile5.1 Motivation for needing a Lockfile

Suppose package.json says "axios": "^1.6.0":Suppose package.json says "axios": "^1.6.0":

Same code, three different results. A lockfile records the exact version of every package, so everyone installs identically.Same code, three different results. A lockfile records the exact version of every package, so everyone installs identically.

ScenarioCommandBehavior
Dev environment syncnpm installReferences lockfile, doesn't upgrade versions
CI / production deploynpm ciStrictly follows lockfile; errors if there's a discrepancy
Active version upgradenpm updateUpgrades within allowed range, updates lockfile

5.2 Should Lockfiles Be Committed to Git5.2 Should Lockfiles Be Committed to Git

Applications must commit; libraries published to npm may not.Applications must commit; libraries published to npm may not.

------

6. Python Virtual Environments6. Python Virtual Environments

Python has a concept that requires special attention: virtual environments (venv).Python has a concept that requires special attention: virtual environments (venv).

Why are they needed?Why are they needed?

Python installs packages globally by default. Your Project A needs requests==2.28, and Project B needs requests==2.31 โ€” they'll conflict.Python installs packages globally by default. Your Project A needs requests==2.28, and Project B needs requests==2.31 โ€” they'll conflict.

Solution: Create an independent virtual environment for each project so they don't interfere with each other.Solution: Create an independent virtual environment for each project so they don't interfere with each other.

bash
# 1. Create a virtual environment (run in project root directory) python -m venv .venv # 2. Activate the virtual environment source .venv/bin/activate # macOS / Linux .venv\Scripts\activate # Windows (Command Prompt CMD) .venv\Scripts\Activate.ps1 # Windows (PowerShell) # 3. After activation, pip install only affects the current virtual environment, not the global one pip install requests # 4. Exit the virtual environment deactivate

> โš ๏ธ Common Windows issue: PowerShell blocks script execution by default. First run:> โš ๏ธ Common Windows issue: PowerShell blocks script execution by default. First run:

> ```powershell> ```powershell

> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

> ```> ```

Modern alternatives:Modern alternatives:

Should .venv be committed to Git?Should .venv be committed to Git?

No! .venv is generated locally and should be added to .gitignore. Use requirements.txt or pyproject.toml to describe dependencies.No! .venv is generated locally and should be added to .gitignore. Use requirements.txt or pyproject.toml to describe dependencies.

------

7. Common Issues Cheat Sheet7. Common Issues Cheat Sheet

Q: Should node_modules be committed to Git?Q: Should node_modules be committed to Git?

No! It's typically hundreds of MB and should be added to .gitignore. With package-lock.json, anyone can quickly rebuild it with npm install.No! It's typically hundreds of MB and should be added to .gitignore. With package-lock.json, anyone can quickly rebuild it with npm install.

Q: Installation fails / getting weird errors?Q: Installation fails / getting weird errors?

bash
# Clear cache, delete old installation, start fresh npm cache clean --force rm -rf node_modules package-lock.json # macOS/Linux rmdir /s /q node_modules && del package-lock.json # Windows CMD npm install

Q: Installation is too slow?Q: Installation is too slow?

bash
# Switch to a domestic mirror (recommended to write to .npmrc file, don't pollute global config) echo "registry=https://registry.npmmirror.com" > .npmrc # pip can also configure mirrors pip install requests -i https://pypi.tuna.tsinghua.edu.cn/simple

Q: How to handle package security vulnerabilities?Q: How to handle package security vulnerabilities?

bash
npm audit # Scan for known vulnerabilities npm audit fix # Auto-fix compatible vulnerabilities npm audit fix --force # Force upgrade (may be breaking, use with caution)

Q: How to tell if a package is trustworthy?Q: How to tell if a package is trustworthy?

Check on [npmjs.com](https://npmjs.com) or [bundlephobia.com](https://bundlephobia.com):Check on [npmjs.com](https://npmjs.com) or [bundlephobia.com](https://bundlephobia.com):

Q: Where does winget install software on Windows?Q: Where does winget install software on Windows?

winget installs to system directories (requires admin) or %LOCALAPPDATA%\Microsoft\WindowsApps by default. Scoop installs all software uniformly in %USERPROFILE%\scoop\apps\, making it easy to manage and migrate.winget installs to system directories (requires admin) or %LOCALAPPDATA%\Microsoft\WindowsApps by default. Scoop installs all software uniformly in %USERPROFILE%\scoop\apps\, making it easy to manage and migrate.

------

8. Terminology Reference8. Terminology Reference

English TermChinese TranslationExplanation
PackageๅŒ… / ๅบ“Code modules written and published by others
Registryๆณจๅ†Œ่กจ / ไป“ๅบ“The central storage server for all packages (e.g., npmjs.com)
Dependencyไพ่ต–Other packages your project needs to run
devDependencyๅผ€ๅ‘ไพ่ต–Packages only needed during development (test frameworks, build tools, etc.)
Lockfile้”ๆ–‡ไปถRecords exact version numbers, ensuring environment consistency
SemVer่ฏญไน‰ๅŒ–็‰ˆๆœฌMAJOR.MINOR.PATCH version naming convention
node_modulesๆจกๅ—็›ฎๅฝ•The directory where npm-installed packages are actually stored
venv่™šๆ‹Ÿ็ŽฏๅขƒAn isolated sandbox for Python project packages
tarballๅŽ‹็ผฉๅŒ…The distribution format of packages, usually .tgz files
Hoistingๆๅ‡npm lifts sub-dependencies to the top level to avoid duplicate installations
Phantom Dependencyๅนฝ็ตไพ่ต–Packages that can be used without being declared in config (pnpm prevents this)
npxโ€”npm's built-in package runner, runs packages temporarily without installing
go.sumโ€”Go module hash verification file, prevents dependency tampering
Crateโ€”The unit name for "packages" in the Rust ecosystem
wingetโ€”Windows' official package manager (built into Windows 10/11)

------

Summary: The Essence of Package ManagersSummary: The Essence of Package Managers

Remember the core with these four points:Remember the core with these four points:

  1. Package manager = app store: Helps you find, install, and manage code parts so you don't reinvent the wheel.Package manager = app store: Helps you find, install, and manage code parts so you don't reinvent the wheel.
  2. Lockfile = team contract: Pins exact versions, making "it works on my machine" a thing of the past.Lockfile = team contract: Pins exact versions, making "it works on my machine" a thing of the past.
  3. Semantic versioning = communication language: ^ safely gets updates; when MAJOR changes, be careful.Semantic versioning = communication language: ^ safely gets updates; when MAJOR changes, be careful.
  4. Local > Global: Install project dependencies locally whenever possible; use npx / uvx for temporary tool runs to keep your environment clean.Local > Global: Install project dependencies locally whenever possible; use npx / uvx for temporary tool runs to keep your environment clean.