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 jest
Alternatively, use the shorthand
pnpm <command>
where supported:bashpnpm jest
Running remote packages without installation (transient execution)
Usepnpm dlx <package>
to fetch temporary packages and run them:bashpnpm dlx create-react-app my-app
Include 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
pnpx
was deprecated in pnpm v6 (2021)- Standalone
npx
was replaced bynpm exec
in 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 dlx
for transient commands (create-*, scaffolding tools) - Prefer
pnpm exec
for 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