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:
- The project requires pnpm (a fast, disk-space efficient package manager) but it’s not installed on your system
- pnpm is installed but not accessible in your current shell
- The project uses pnpm-specific configurations or lockfiles
- 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.
Recommended Solutions
1. Install pnpm Globally via npm
npm install -g pnpm
After installation, verify with:
pnpm -v # Should display version number
Best for:
- Users needing quick setup
- Environments without Corepack
- Node.js versions below 16.9
2. Enable pnpm Using Corepack (Modern Recommended Approach)
corepack enable pnpm
Windows Permission Requirements
On Windows, run PowerShell as Administrator:
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 ofpackage-lock.json
- npm scripts trigger
pnpm
commands listed inpackage.json
(e.g.,"dev": "pnpm next dev"
) - Corepack (bundled with recent Node.js versions) provides integrated package manager management
Solution-Specific Benefits
Solution | Key Advantage | Common Use Case |
---|---|---|
npm install -g pnpm | Simpler execution | Individual developer setups |
corepack enable pnpm | No global install needed Version consistency | Team environments CI/CD pipelines |
Advanced Troubleshooting
Alternative: Install via Package Managers
# Homebrew (macOS/Linux)
brew install pnpm
# Volta (version manager)
volta install pnpm
Validate Shell Configuration
Ensure your shell's PATH includes pnpm's binaries:
# 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+):
- Use Corepack (no separate installation required)
- Run
corepack prepare pnpm@latest --activate
for specific versions
For legacy systems:
- Install via
npm install -g pnpm
- Manually add installation directory to PATH if required
Project Portability Tip
Include Corepack in your project initialization:
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
.