Published on

Difference Between pnpm create, pnpx, and dlx

Authors
  • Name
    Ripal & Zalak
    Twitter

Difference Between pnpm create, pnpx, and dlx

When using pnpm, you might encounter commands like pnpm create, pnpx, and pnpm dlx. While they may seem similar, they have distinct purposes and use cases. This guide explains their differences, use cases, and best practices.

Overview of the Commands

  1. pnpm exec:

    • Searches for a binary locally in node_modules/.bin and runs it.
    • If the binary is not found locally, the command will fail.
    • Example:
      pnpm exec jest
      
  2. pnpm dlx:

    • Downloads a package remotely from the registry and runs its binary.
    • The package is not added as a project dependency.
    • Example:
      pnpm dlx create-react-app my-app
      
  3. pnpm create:

    • A shorthand for pnpm dlx create-<first_argument>.
    • Specifically designed for creating new apps or projects from templates.
    • Example:
      pnpm create react-app my-app
      
  4. pnpx:

    • Deprecated in favor of pnpm dlx.
    • Behaves like npx, running either locally installed binaries or downloading and running them from the registry if not found locally.
  5. npx:

    • Searches for binaries in node_modules/.bin or downloads them remotely if not found.
    • Example:
      npx create-react-app my-app
      

Key Differences

CommandPurposeBehavior
pnpm execRuns binaries from local node_modulesFails if the binary is not found locally.
pnpm dlxRuns binaries downloaded from the registryDoes not add the package to dependencies.
pnpm createCreates new projectsShorthand for pnpm dlx create-<first_argument>.
pnpxDeprecatedAlias for pnpm dlx, combines exec and dlx behaviors.
npxRuns local or remote binariesAutomatically downloads if not found locally.

When to Use Each Command

Use pnpm exec for:

  • Running scripts or tools included in your package.json dependencies.
  • Example: Running jest or eslint installed locally.

Use pnpm dlx for:

  • Executing CLI tools from the npm registry without adding them as dependencies.
  • Example: Running a one-off tool like http-server.

Use pnpm create for:

  • Initializing new projects or applications using boilerplate templates.
  • Example: Bootstrapping a React project with pnpm create react-app.

Avoid pnpx:

  • Use pnpm dlx instead, as pnpx is deprecated.

Use npx (if not using pnpm):

  • Running one-off commands in projects that use npm instead of pnpm.

Best Practices

  • Prefer pnpm create for setting up new projects from templates.
  • Use pnpm dlx for running tools directly from the registry without adding dependencies.
  • Always commit pnpm-lock.yaml for reproducible builds.

FAQs

Why is pnpx deprecated?

pnpx was replaced by pnpm dlx to separate the functionality of running local binaries (pnpm exec) and remote binaries (pnpm dlx). This separation aligns with pnpm's philosophy of clarity and explicitness.

What is the difference between pnpm exec and pnpm dlx?

  • pnpm exec only runs locally installed binaries.
  • pnpm dlx downloads and runs binaries from the registry without installing them as dependencies.

When should I use pnpm create instead of pnpm dlx?

Use pnpm create when creating new projects, as it automatically maps to templates like create-react-app or create-svelte.

Conclusion

Understanding the differences between pnpm create, pnpx, and pnpm dlx is essential for efficient project management and development. By choosing the right command for your task, you can streamline your workflow and maintain clarity in your projects.