npx with pnpm Equivalents
Problem Statement
When transitioning from npm to pnpm, developers often wonder whether they can use the familiar npx command to execute both locally installed binaries and remote packages. While npx doesn't integrate seamlessly with pnpm projects, and the deprecated pnpx tool fetches remote dependencies instead of local binaries, pnpm provides dedicated solutions for these workflows.
Solution
pnpm offers two distinct commands that replace npx's functionality, each addressing a specific use case:
Executing locally installed binaries
Usepnpm exec <command>to run binaries from your project'snode_modules:bashpnpm exec jestAlternatively, use the shorthand
pnpm <command>where supported:bashpnpm jestRunning remote packages without installation (transient execution)
Usepnpm dlx <package>to fetch temporary packages and run them:bashpnpm dlx create-react-app my-appInclude version specifiers and package-specific flags:
bashpnpm dlx create-next-app@latest --use-pnpm
Key Differences Explained
| Use Case | npx | pnpm Equivalent |
|---|---|---|
| Execute local binaries | npx <binary> | pnpm exec <binary> or pnpm <binary> |
| Run remote packages | npx <remote-package> | pnpm dlx <remote-package> |
Avoid Deprecated Tools
pnpxwas deprecated in pnpm v6 (2021)- Standalone
npxwas replaced bynpm execin npm v7+
Practical Workflow Example
- Install a development tool with
pnpm:bashpnpm add -D typescript - Run the local binary:bash
pnpm exec tsc --version # Or shorthand: pnpm tsc --version - Execute remote packages without installing:bash
pnpm dlx json-server db.json
Best Practices Recommendation
- Default to
pnpm dlxfor transient commands (create-*, scaffolding tools) - Prefer
pnpm execfor local binaries to avoid namespace conflicts - Use
pnpm <binary>shorthand only when certain the command won't collide with pnpm built-ins (e.g.,install,add)
For legacy projects using both managers, explicitly declare the package manager in installation commands:
npx create-next-app@latest --use-pnpm