Published on

How do you switch between pnpm versions?

Authors
  • Name
    Ripal & Zalak
    Twitter

How do you switch between pnpm versions?

Managing multiple versions of pnpm is essential when working on projects with differing version requirements. This guide will walk you through various methods to switch between pnpm versions efficiently.

Why Switch Between pnpm Versions?

Different projects may depend on specific versions of pnpm due to:

  • Compatibility with the lockfile format.
  • Dependencies that work only with a particular pnpm version.
  • Team or organizational standards for package management tools.

Switching pnpm versions ensures your workflow aligns with project-specific requirements.

Methods to Switch Between pnpm Versions

1. Using Corepack

Corepack is a Node.js tool that manages versions of package managers, including pnpm. Here’s how to use it:

  • Enable Corepack:

    corepack enable
    
  • Activate a Specific Version:

    corepack prepare pnpm@<version> --activate
    

    Example:

    corepack prepare [email protected] --activate
    
  • Use the Latest Version:

    corepack prepare pnpm@latest --activate
    

2. Specify pnpm Version in package.json

You can specify a project-specific pnpm version in the package.json file:

{
  "name": "my-project",
  "packageManager": "[email protected]"
}

When you run pnpm install or any pnpm command, Corepack will ensure the specified version is used for that project.

3. Using npx for One-Off Commands

If you want to run a specific version of pnpm temporarily, use npx:

npx pnpm@<version> <command>

Example:

npx pnpm@6 install

This approach is useful for testing or contributing to projects that require different pnpm versions.

4. Global Installation via Command Line

You can globally install a specific version of pnpm using:

pnpm install -g pnpm@<version>

Example:

pnpm install -g [email protected]

5. Using Aliases for Quick Switching

Set up aliases in your shell configuration file (e.g., .bashrc, .zshrc) for quick switching:

alias pnpm7='corepack prepare [email protected] --activate'
alias pnpmlatest='corepack prepare pnpm@latest --activate'

Reload your shell, and you can switch versions with:

pnpm7
pnpmlatest

FAQs

What is Corepack?

Corepack is a Node.js utility for managing package manager versions (e.g., pnpm, yarn, npm). It allows you to activate and use specific versions without global installations.

Can I use different versions for different projects?

Yes, by specifying the pnpm version in the package.json file or activating specific versions with Corepack.

How do I revert to the default pnpm version?

Run the following command to reset to the default version installed globally:

corepack disable

Best Practices

  • Use Corepack: It simplifies managing multiple versions without cluttering global installations.
  • Specify Versions in package.json: This ensures all team members use the same pnpm version.
  • Test Changes with npx: Use npx for temporary version switching and testing.

Conclusion

Switching between pnpm versions is a straightforward process with tools like Corepack, npx, and project-specific configurations. By following these practices, you can manage version dependencies effectively and maintain a smooth development workflow.