Skip to content

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:

  1. Executing locally installed binaries
    Use pnpm exec <command> to run binaries from your project's node_modules:

    bash
    pnpm exec jest

    Alternatively, use the shorthand pnpm <command> where supported:

    bash
    pnpm jest
  2. Running remote packages without installation (transient execution)
    Use pnpm dlx <package> to fetch temporary packages and run them:

    bash
    pnpm dlx create-react-app my-app

    Include version specifiers and package-specific flags:

    bash
    pnpm dlx create-next-app@latest --use-pnpm

Key Differences Explained

Use Casenpxpnpm Equivalent
Execute local binariesnpx <binary>pnpm exec <binary> or pnpm <binary>
Run remote packagesnpx <remote-package>pnpm dlx <remote-package>

Avoid Deprecated Tools

  • pnpx was deprecated in pnpm v6 (2021)
  • Standalone npx was replaced by npm exec in npm v7+

Practical Workflow Example

  1. Install a development tool with pnpm:
    bash
    pnpm add -D typescript
  2. Run the local binary:
    bash
    pnpm exec tsc --version
    # Or shorthand:
    pnpm tsc --version
  3. 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:

bash
npx create-next-app@latest --use-pnpm