乐闻世界logo
搜索文章和话题

How to change where pnpm installs the global packages?

1个答案

1

When installing global packages with pnpm, by default, it installs them to a specific global directory. However, if you need to change the installation location for global packages, you can achieve this by setting environment variables.

For pnpm, the method to change the global package installation location is as follows:

  1. Set environment variables: You need to set the PNPM_HOME environment variable to specify the installation directory for globally installed packages. Additionally, ensure that the ${PNPM_HOME}/bin directory is added to your PATH environment variable so that you can run these globally installed packages from any location.

  2. Update configuration files: If you do not want to set the environment variables every time you open a new terminal or session, you can add these environment variables to your shell configuration file (such as .bashrc, .bash_profile, .zshrc, etc.) so that they are automatically loaded.

Below are the steps to set these environment variables on Unix-like systems (e.g., Linux or macOS):

sh
# Set the PNPM_HOME environment variable to specify the new location for globally installed packages export PNPM_HOME="/path/to/your/global/pnpm/location" # Add the PNPM_HOME/bin directory to the PATH environment variable export PATH="$PNPM_HOME/bin:$PATH" # Add these lines to your shell configuration file, such as .bashrc or .zshrc, # to ensure that these variables are set in every new terminal session. echo 'export PNPM_HOME="/path/to/your/global/pnpm/location"' >> ~/.bashrc echo 'export PATH="$PNPM_HOME/bin:$PATH"' >> ~/.bashrc

For Windows systems, you can set the environment variables via the System Properties under Environment Variables or through command line (e.g., using PowerShell):

powershell
# Set the PNPM_HOME environment variable to specify the new location for globally installed packages $env:PNPM_HOME="C:\path\to\your\global\pnpm\location" # Add the PNPM_HOME\bin directory to the existing PATH environment variable $env:PATH="$env:PNPM_HOME\bin;$env:PATH" # To permanently set the environment variables, use Set-ItemProperty Set-ItemProperty -Path 'HKCU:\Environment' -Name 'PNPM_HOME' -Value 'C:\path\to\your\global\pnpm\location' [System.Environment]::SetEnvironmentVariable('PATH', "$env:PNPM_HOME\bin;$env:PATH", [System.EnvironmentVariableTarget]::User)

After setting these environment variables, when you use the pnpm install -g <package> command to install any global package, pnpm will install them to the new location you specified. Remember to replace /path/to/your/global/pnpm/location and C:\path\to\your\global\pnpm\location with the actual path where you want the global packages to be installed.

2024年6月29日 12:07 回复

你的答案