Skip to content

Resolving "pnpm: command not found" Error

Problem Statement

When working with a Next.js project cloned from a repository, you may encounter the error sh: pnpm: command not found after running commands like npm run dev. This error typically occurs because:

  1. The project requires pnpm (a fast, disk-space efficient package manager) but it’s not installed on your system
  2. pnpm is installed but not accessible in your current shell
  3. The project uses pnpm-specific configurations or lockfiles
  4. No automatic fallback to npm/yarn is implemented in the project scripts

The error appears even after running npm install, as the core issue lies with the package manager itself rather than project dependencies.

1. Install pnpm Globally via npm

bash
npm install -g pnpm

After installation, verify with:

bash
pnpm -v  # Should display version number

Best for:

  • Users needing quick setup
  • Environments without Corepack
  • Node.js versions below 16.9
bash
corepack enable pnpm

Windows Permission Requirements

On Windows, run PowerShell as Administrator:

powershell
corepack enable pnpm

When prompted to install pnpm, enter Y.

Best for:

  • Node.js v16.9+ (Corepack included by default)
  • Projects maintaining multiple Node/package manager versions
  • Standardized environments managed through Node.js

Why These Solutions Work

Background Analysis

  • pnpm isn't a native system tool and requires separate installation
  • Cloned projects may inherit pnpm-lock.yaml files instead of package-lock.json
  • npm scripts trigger pnpm commands listed in package.json (e.g., "dev": "pnpm next dev")
  • Corepack (bundled with recent Node.js versions) provides integrated package manager management

Solution-Specific Benefits

SolutionKey AdvantageCommon Use Case
npm install -g pnpmSimpler executionIndividual developer setups
corepack enable pnpmNo global install needed
Version consistency
Team environments
CI/CD pipelines

Advanced Troubleshooting

Alternative: Install via Package Managers

bash
# Homebrew (macOS/Linux)
brew install pnpm

# Volta (version manager)
volta install pnpm

Validate Shell Configuration

Ensure your shell's PATH includes pnpm's binaries:

bash
# Check installation location
which pnpm  # Linux/macOS
where pnpm  # Windows

# Add to PATH if missing (typical locations)
export PATH="$HOME/.local/share/pnpm:$PATH"  # Add to .bashrc/.zshrc

Choosing the Right Approach

For new Node.js installations (v16.9+):

  1. Use Corepack (no separate installation required)
  2. Run corepack prepare pnpm@latest --activate for specific versions

For legacy systems:

  1. Install via npm install -g pnpm
  2. Manually add installation directory to PATH if required

Project Portability Tip

Include Corepack in your project initialization:

bash
corepack enable
pnpm install

This ensures collaborators automatically use pnpm without manual configuration.

After resolving the "command not found" issue, run pnpm install to install project-specific dependencies using pnpm before executing pnpm run dev.