npm vs pnpm vs npx vs yarn

1. npm (Node Package Manager)

  • Comes by default when you install Node.js.
  • Used to install packages, manage versions, and run scripts.

🔹 Example: Install React

npm install react

🔹 Install globally:

npm install -g typescript

🔹 Run project scripts (from package.json):

npm run build

2. npx (Node Package eXecute)

  • Comes bundled with npm (v5.2+).
  • Lets you run a package without installing it globally.
  • Useful for CLI tools you only need occasionally.

🔹 Example: Run create-react-app without installing it:

npx create-react-app my-app

➡️ This downloads the package temporarily, runs it, then removes it.


3. Yarn

  • An alternative to npm, developed by Facebook.
  • Faster installs due to caching & parallel downloads.
  • Uses yarn.lock instead of package-lock.json.

🔹 Example: Install dependencies

yarn install

🔹 Add a package

yarn add axios

🔹 Run scripts

yarn build

4. pnpm (Performant npm)

  • A newer, faster, disk-efficient package manager.
  • Uses symlinks and a global store to avoid duplicating dependencies across projects.
  • Saves a lot of space compared to npm/yarn.

🔹 Example: Install dependencies

pnpm install

🔹 Add a package

pnpm add lodash

🔹 Run scripts

pnpm run start

Quick Comparison

ToolPurposeKey Feature
npmDefault package managerStable, widely used
npxRun packages without installTemporary execution
YarnPackage managerFaster installs, better caching
pnpmPackage managerEfficient disk usage, faster dependency handling

👉 Example: Installing React in different managers

npm install react
yarn add react
pnpm add react

👉 Example: Running create-react-app

npx create-react-app my-app   # No global install

Leave a Reply