Published on

Can I Use npx with pnpm?

Authors
  • Name
    Ripal & Zalak
    Twitter

Can I Use npx with pnpm?

npx is a widely used tool in the npm ecosystem for running binaries and commands. If you’ve switched to pnpm as your package manager, you might wonder whether you can use npx alongside it or if there are alternatives. This guide will clarify how to achieve similar functionality with pnpm.


Key Uses of npx

The two primary use cases of npx are:

  1. Running Executables from Local Dependencies:
    • Example: npx jest
  2. Running Executables from Packages You Want to Download Transiently:
    • Example: npx create-react-app my-app

Let’s explore how these scenarios translate to pnpm.


The pnpm Alternatives

1. Running Executables Inside Local Dependencies

If you want to run a binary from a locally installed dependency, use:

pnpm exec <command>

For example, to run jest:

pnpm exec jest

Alternatively, you can omit exec for simplicity:

pnpm jest

This approach works similarly to npx jest in npm.

2. Running Executables from Remote Packages

For one-time usage of a remote package, the pnpm equivalent of npx <package> is:

pnpm dlx <package>

Example:

pnpm dlx create-react-app my-app

The dlx command downloads the package temporarily, executes it, and then removes it after use. This is equivalent to the transient nature of npx.


Transitioning from pnpx

pnpx was a command similar to npx, provided by pnpm. However, as of recent versions of pnpm, pnpx has been deprecated and replaced by pnpm dlx.

Why the Change?

The dlx command is more aligned with modern use cases, offering:

  • Better argument parsing.
  • Streamlined execution of remote packages.

Practical Examples

Running Local Binaries

Install a local dependency that provides a binary:

pnpm add -D jest

Run the binary:

pnpm exec jest

Or, simply:

pnpm jest

Running a Remote Package

Execute a remote binary without installation:

pnpm dlx create-next-app my-next-app

Running Remote Scripts

Run an arbitrary package:

pnpm dlx @scaffold-eth/create

Frequently Asked Questions

1. Can I use npx with pnpm?

While npx is part of npm, its functionality is better handled in pnpm using pnpm exec and pnpm dlx.

2. Why was pnpx deprecated?

pnpx was replaced by pnpm dlx for better clarity and improved argument handling. pnpm dlx is now the recommended way to run remote binaries.

3. What are the key differences between pnpm exec and pnpm dlx?

  • pnpm exec: Runs binaries from locally installed dependencies.
  • pnpm dlx: Downloads, executes, and removes a package temporarily.

4. Can I alias pnpm exec or pnpm dlx?

Yes, you can create aliases for convenience:

alias pexec="pnpm exec"
alias pdlx="pnpm dlx"

Conclusion

While npx is not directly usable with pnpm, the commands pnpm exec and pnpm dlx provide equivalent and more streamlined functionality. Transitioning to these commands ensures a smooth experience with pnpm.