- Published on
Difference Between pnpm create, pnpx, and dlx
- Authors
- Name
- Ripal & Zalak
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
pnpm exec:- Searches for a binary locally in
node_modules/.binand runs it. - If the binary is not found locally, the command will fail.
- Example:
pnpm exec jest
- Searches for a binary locally in
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
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
- A shorthand for
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.
- Deprecated in favor of
npx:- Searches for binaries in
node_modules/.binor downloads them remotely if not found. - Example:
npx create-react-app my-app
- Searches for binaries in
Key Differences
| Command | Purpose | Behavior |
|---|---|---|
pnpm exec | Runs binaries from local node_modules | Fails if the binary is not found locally. |
pnpm dlx | Runs binaries downloaded from the registry | Does not add the package to dependencies. |
pnpm create | Creates new projects | Shorthand for pnpm dlx create-<first_argument>. |
pnpx | Deprecated | Alias for pnpm dlx, combines exec and dlx behaviors. |
npx | Runs local or remote binaries | Automatically downloads if not found locally. |
When to Use Each Command
Use pnpm exec for:
- Running scripts or tools included in your
package.jsondependencies. - Example: Running
jestoreslintinstalled 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 dlxinstead, aspnpxis deprecated.
Use npx (if not using pnpm):
- Running one-off commands in projects that use
npminstead ofpnpm.
Best Practices
- Prefer
pnpm createfor setting up new projects from templates. - Use
pnpm dlxfor running tools directly from the registry without adding dependencies. - Always commit
pnpm-lock.yamlfor 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 execonly runs locally installed binaries.pnpm dlxdownloads 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.
