Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pnpm v7] crashes "add --global" with "ERROR  Unable to find the global bin directory" #4658

Closed
lisunovdv opened this issue May 3, 2022 · 78 comments

Comments

@lisunovdv
Copy link

add --global does not work without additional configuration

pnpm version: 7.0.0

Code to reproduce the issue:

npm install --global pnpm
pnpm add --global npm-run-all
ERROR  Unable to find the global bin directory
Run "pnpm setup" to create it automatically, or set the global-bin-dir setting, or the PNPM_HOME env variable. The global bin directory should be in the PATH.

Expected behavior:

pnpm add --global <module> works correctly without additional configurations

Actual behavior:

It causes an error and needs a workaround on CI

Additional information:

##[debug] Azure DevOps
##[debug] vmImage: ubuntu-20.04
##[debug]script=npm install --global pnpm
pnpm config set store-dir /home/vsts/work/1/.pnpm-store
##[debug]pnpm -v
7.0.0
##[debug]node -v
v16.14.0
@lisunovdv lisunovdv changed the title pnpm 7.0.0 crashes "add --global" with "ERROR  Unable to find the global bin directory" [pnpm v7] crashes "add --global" with "ERROR  Unable to find the global bin directory" May 3, 2022
@josefaidt
Copy link

Hello I also began seeing this issue with pnpm v7. Using this with fnm we can manually set PNPM_HOME to point to the same location as npm bin -g, but previously this pointed to a cache that's loaded from npm's bin directory(?).

josef ~
> fnm use 16
Using Node v16.14.2

josef ~
> npm bin -g
/Users/josef/.fnm/node-versions/v16.14.2/installation/bin
(not in PATH env variable)

josef ~
> pnpm -v
6.32.11

josef ~
> pnpm bin -g
/Users/josef/Library/Caches/fnm_multishells/96870_1651591328965/bin

josef ~
> pnpm add -g pnpm
Nothing to stop. No server is running for the store at /Users/josef/.pnpm-store/v3
Packages: +1 -1
+-
Progress: resolved 27, reused 26, downloaded 1, added 1, done

/Users/josef/.fnm/node-versions/v16.14.2/installation/pnpm-global/5:
- pnpm 6.32.11
+ pnpm 7.0.0

josef ~
> pnpm bin -g
 ERROR  Unable to find the global bin directory
Run "pnpm setup" to create it automatically, or set the global-bin-dir setting, or the PNPM_HOME env variable. The global bin directory should be in the PATH.

@zkochan
Copy link
Member

zkochan commented May 3, 2022

This works as expected.

If you use the standalone script to install pnpm, then it will add PNPM_HOME dir to your PATH. Otherwise, you may run pnpm setup, as noted in the error message.

We had to remove "autodetection" of global bin location because it was causing more trouble than good.

@lisunovdv
Copy link
Author

Hi @zkochan Maybe it needs to update the installation guide?

@laoshaw

This comment was marked as off-topic.

@caveman-dick
Copy link

Unfortunately, this has caused me a big headache with our Azure DevOps CI builds. Trying to fix using pnpm setup doesn't work as it only updates the bashrc file with the new settings and doesn't update the currently running shell. I've tried manually reloading the bashrc file but that doesn't seem to work inside the script. The only way I could get this to work was to dig through the code to work out what the pnpm setup command was doing and manually set the PNPM_HOME and PATH variables in the script block:

- script: |
      export PNPM_HOME="/home/vsts/.local/share/pnpm"
      export PATH="$PNPM_HOME:$PATH"
      pnpm add -g aws-cdk
    displayName: 'Install AWS CDK'

This obviously is pretty painful and will need to be done for every script that is doing a global install as part of the pipeline.

@zkochan you mentioned above that the automatic detection of the global store needed to be removed, what problems was it causing before? Is there a better workaround that I could use?

@zkochan
Copy link
Member

zkochan commented May 4, 2022

I suddenly have issues with ts-node(npm works fine):

This is not related to this issue. This is about missing or incorrect peer dependencies. You need to install @types/node and typescript globally because they are needed by ts-node.

@zkochan
Copy link
Member

zkochan commented May 4, 2022

@caveman-dick why do you need global packages?

@zkochan you mentioned above that the automatic detection of the global store needed to be removed, what problems was it causing before? Is there a better workaround that I could use?

There were many problems. Dozens of issues with dozens of messages. Some of the issues were that pnpm did not have write access to the global directory. Some issues were that pnpm was writing to a directory that the users didn't want to write to. Some issues were about pnpm overriding bins written by other package managers.

In general, I would recommend not using global packages in CI. Your dependencies should be declared as dev dependencies of the project.

@pnpm pnpm deleted a comment from laoshaw May 5, 2022
@caveman-dick
Copy link

@zkochan we use the aws-cdk which just installs a cmd line tool. Locally this works fine without any global install, it picks it up from the node_modules/.bin folder, but on the CI server for some reason, this doesn't work. The only other way to get it to work is to use npx aws-cdk:

image

unfortunately pnpx aws-cdk (or pnpm dlx aws-cdk) doesn't work:

image

possibly due to the mismatch that the package is called aws-cdk but the executable is called cdk? 🤷‍♂️

Ideally, we would like to use the same package manager for all tasks to save confusion and hence why we have historically done a global install of the package. It also obviously saves having to prefix every subsequent cdk command as we run several as part of the pipeline.

@aparajita
Copy link
Contributor

aparajita commented May 5, 2022

@zkochan, I honestly can't understand the logic behind this implementation of the global bin dir. By default, the global bin directory is set to a subdirectory of the current user. How can that make sense? Other users won't be able to access or write to that directory, which on servers is a real problem. Is it your intention that each user has their own copy of the binaries?

If I set the global bin dir to a shared directory, like /usr/local/share/pnpm, pnpm absolutely refuses to find it. PNPM_HOME is definitely set and in PATH.

Any suggestions?

@joeldenning
Copy link

fwiw running pnpm setup didn't get rid of this error for me. To fix, I created a directory /Users/joeldenning/pnpm then added the following to my zshrc:

export PATH="/Users/joeldenning/pnpm:$PATH"
export PNPM_HOME="/Users/joeldenning/pnpm"

@zkochan
Copy link
Member

zkochan commented May 6, 2022

I honestly can't understand the logic behind this implementation of the global bin dir. By default, the global bin directory is set to a subdirectory of the current user. How can that make sense?

This is how most other tools work (volta, nvm, etc). This is how users suggested to do it

If you think it should work differently, let's discuss it in a separate discussion.

fwiw running pnpm setup didn't get rid of this error for me.

This looks like a bug. Was there any output?

@Deebster
Copy link

Deebster commented May 8, 2022

I also find that pnpm setup is not fixing the issue (on Win10).

(C:\Users\micro\AppData\Local\pnpm exists and is accessible and C:\Users\micro\AppData\Local\pnpm\store is populated from previous installs)

PS C:\Users\micro> pnpm add -g pnpm
 ERROR  Unable to find the global bin directory
Run "pnpm setup" to create it automatically, or set the global-bin-dir setting, or the PNPM_HOME env variable. The global bin directory should be in the PATH.
PS C:\Users\micro> pnpm setup
Currently 'PNPM_HOME' is set to 'C:\Users\micro\AppData\Local\pnpm'
PATH already contains PNPM_HOME

Setup complete. Open a new terminal to start using pnpm.

// new teminal

PowerShell 7.2.3
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS C:\Users\micro> pnpm -v
7.0.0
PS C:\Users\micro> pnpm add -g pnpm
 ERROR  Unable to find the global bin directory
Run "pnpm setup" to create it automatically, or set the global-bin-dir setting, or the PNPM_HOME env variable. The global bin directory should be in the PATH.
PS C:\Users\micro> $env:PNPM_HOME = "C:\Users\micro\AppData\Local\pnpm"
PS C:\Users\micro> pnpm add -g pnpm
 ERROR  The configured global bin directory "C:\Users\micro\AppData\Local\pnpm" is not in PATH
PS C:\Users\micro> $env:Path += ";C:\Users\micro\AppData\Local\pnpm"
PS C:\Users\micro> pnpm add -g pnpm
Nothing to stop. No server is running for the store at C:\Users\micro\AppData\Local\pnpm\store\v3
The location of the currently running pnpm differs from the location where pnpm will be installed
 Current pnpm location: C:\Users\micro\AppData\Roaming\npm
 Target location: C:\Users\micro\AppData\Local\pnpm

Packages: +1
+
Packages are hard linked from the content-addressable store to the virtual store.
  Content-addressable store is at: C:\Users\micro\AppData\Local\pnpm\store\v3
  Virtual store is at:             .pnpm

C:\Users\micro\AppData\Local\pnpm\global\5:
+ pnpm 7.0.1

Progress: resolved 1, reused 0, downloaded 1, added 1, done
PS C:\Users\micro> cd C:\code\svelte\
PS C:\code\svelte> pnpm init svelte@next test
Wrote to C:\code\svelte\package.json

{
  "name": "svelte",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
PS C:\code\svelte> pnpm -v
7.0.0
PS C:\code\svelte>

However, a restart seems to have fixed it:

PS C:\Users\micro> cd \code\svelte
PS C:\code\svelte> pnpm init svelte@next tesht
 ERR_PNPM_INIT_ARG  init command does not accept any arguments

Maybe you wanted to run "pnpm create svelte@next tesht"
PS C:\code\svelte> pnpm -v
7.0.1

@zkochan
Copy link
Member

zkochan commented May 9, 2022

fwiw running pnpm setup didn't get rid of this error for me. To fix, I created a directory /Users/joeldenning/pnpm then added the following to my zshrc:

Looks like pnpm setup cannot detect that the current shell is zsh. It check the value of $SHELL env variable but it says bash even on zsh. I think we can check if $ZSH_VERSION is set instead.

@zkochan
Copy link
Member

zkochan commented May 9, 2022

Maybe this should improve it: #4701

@arjittw
Copy link

arjittw commented May 9, 2022

I my case, pnpm's global bin directory changed and it has created C:\Users\arjit\AppData\Local\pnpm\global\5
but not node_modules folder in it.
It needs the node_modules in it like this C:\Users\arjit\AppData\Local\pnpm\global\5\node_modules.
So it throws error unable to find global bin directory, Solved after installing anything globally like this $ pnpm add -g <anything>. This has created node_modules folder.
But I have to manually delete v6 global bin and reinstall all packages.

The problem is pnpm changed global bin path so all modules just ignored like a fresh install.

@chioio
Copy link

chioio commented May 10, 2022

I use the brew to install pnpm v7.0.1, and add global module occurred this error.
I solved by these steps:

  1. Remove PNPM_HOME variable in .zshrc
  2. Run pnpm setup
  3. Cat .zshrc, find PNPM_HOME variable.
  4. Run source .zshrc

Hope this can help you!

@zkochan
Copy link
Member

zkochan commented May 10, 2022

Why were you removing PNPM_HOME from .zshrc? Did you have a bad path there? I guess we need to update pnpm setup, so that it can update PNPM_HOME, if it is already set but incorrectly

@ZYinMD
Copy link

ZYinMD commented May 10, 2022

A boring log of what happened to me, for information only.

  1. pnpm add -g pnpm to update to v7.0.0
  2. got reminded v7.0.1 became available
  3. pnpm add -g pnpm to update to v7.0.1, encountered this issue
  4. per instruction on screen: pnpm setup
  5. pnpm add -g pnpm again in a new terminal, issues disappears, now at v7.0.1

@apottere

This comment was marked as resolved.

@lizy0329-biubiubiu
Copy link

"Open a new terminal to start using pnpm" is work for me.

@RafidMuhymin
Copy link

I am getting the same error when running pnpm list -g. And the trick of opening a new terminal/tab to use pnpm worked for me too.

@zkochan

This comment was marked as resolved.

@qiutian00
Copy link

"Open a new terminal to start using pnpm" is work for me.

Thank you!

@cjosue15
Copy link

cjosue15 commented Oct 31, 2022

I use windows terminal with powershell on windows10-19044, and that's my solution:

  1. run pnpm setup
  2. add PNPM_HOME=C:\Users\[your system username]\AppData\Local\pnpm to global system variables, not user variables!!!
  3. add %PNPM_HOME% to Path of system variables
  4. restart windows terminal

image

2022/09/22, update for macos-12.2.1 solutions:

  1. run pnpm setup, this will print PNPM_HOME and PATH in console, such as:
export PNPM_HOME="/Users/xxxx/Library/pnpm"
export PATH="$PNPM_HOME:$PATH"
  1. copy these two statements and paste them to ~/.zshrc file
open ~/.zshrc
source ~/.zshrc
image
  1. restart a new terminal

thats solved my problem on windows 11, thanks :)

@TemaSM
Copy link

TemaSM commented Nov 5, 2022

@0xlsca so the issue is when someone installs pnpm with npm and then tries to manually set PNPM_HOME on Windows? What are the steps to reproduce the issue and what would be the fix?

@zkochan Hi 👋
Confirming issue on 3 Windows PCs. Here's how you can reproduce it (attached video of repro below):

  1. use fresh Windows (just for clarity of experiment)
  2. install Node LTS (via official .msi, or by using: nvs / nvm-windows
  3. npm i -g pnpm
  4. install any package with -g flag, example: pnpm i -g npm-check-updates
  5. ERROR  Unable to find the global bin directory ... Run "pnpm setup" to create it automatically ...
  6. pnpm setup // ENVs got updated
  7. restart terminal
  8. try to install any package with -g flag again
  9. boom, error:
     ERROR  The configured global bin directory "C:\Users\User\AppData\Local\pnpm" is not in PATH

node: v18.12.1
npm: v8.19.2
pnpm: v7.14.2

Video demonstration (skip to 0:50):
pnpm_error.mp4

Hope it will help you 😃

UPD: pnpm v7.9.0 doesn't have this problem, but v7.9.1 introduces it probably by:
8cb47ac
pnpm/components@4b4da10
Probably related to: #4744

And here's why (one of these variants):

  1. Performed update in ENVs via Windows registry not recognized immediately and refreshEnvVars not working as expected while setting PNPM_HOME for the first time
  2. PNPM_HOME was set as Expandable string (REG_EXPAND_SZ) but should be just String (REG_SZ)
Video demo of fix (changing type of env right after `pnpm setup`):
pnpm_error_fix.mp4

@edgexie
Copy link

edgexie commented Nov 13, 2022

I have the same problem

@Kbdq
Copy link

Kbdq commented Nov 18, 2022

I have same problem - "ERROR  The configured global bin directory "C:\Users\User\AppData\Local\pnpm" is not in PATH"
I have deleted and recreated PNPM_HOME environment variable (with the same content it had before) and it solved the issue for me.

@dawidmachon
Copy link

dawidmachon commented Nov 20, 2022

This solving problems with update.
Important, do not skip opening a new terminal.

  1. Run pnpm setup
  2. Open a new terminal, run pnpm add -g pnpm
  3. Open a new terminal, run pnpm --version, got a new version.

@zkochan
Copy link
Member

zkochan commented Nov 27, 2022

PNPM_HOME was set as Expandable string (REG_EXPAND_SZ) but should be just String (REG_SZ)

This is done in v7.17.1
Let me know if it helped.

Also, there is a related issue here: #5696

@dfahlander
Copy link

I'm also stuck with this. I haven't messed up with any env vars or so - just installed pnpm using npm some months ago. No manual changes of anything. It's been working great! But today I've got the following message while installing a dependency to a new package:

image

Ok, so I follow the advice and:

image

Don't know if the fact that I'm using nvm to switch between different versions of node could possibly be triggering changes in the global npm environment and affect pnpm?

What's the easiest workarond for this? Setting the PNPM_HOME dir to what? How do I find that out? Or should I rather just reinstall pnpm using the standalone script instead of using npm ?

@amir-khoshbakht
Copy link

amir-khoshbakht commented Jan 11, 2023

dont know if its related or not.
(Windows 10)
when i tried to add a package with -g flag "pnpm add -g" I got this error :
ERROR  The configured global bin directory "C:\Users................\AppData\Local\pnpm" is not in PATH
running terminal with Run as admin solved the problem

@userAugustos
Copy link

Run as admin

Since as admin u can run, looks like u need admin role to install globally with your pnpm, what may be not good, try to use the windows terminal with powershell or, run this directly:

$PNPM_HOME="C:\Users\user\AppData\Local\pnpm" | pnpm add -g

to try to set your PATH

@jason-ha
Copy link

PNPM_HOME was set as Expandable string (REG_EXPAND_SZ) but should be just String (REG_SZ)

This is done in v7.17.1 Let me know if it helped.

Also, there is a related issue here: #5696

I have confirmed that 7.17.1 from a clean environment will get things properly configured.
However, if you run pnpm setup using 7.17.1+ one a Windows system that already has PNPM_HOME established, it will remain broken.

@AdvTechnoKing
Copy link

It's super funny moment

In system settings is ok:
image
image

But if you call echo %PATH% you will see the next:
image
image

IDK why it happened


The solution is simple, replace %PNPM_HOME% with a normal path C:\Users\u\AppData\Local\pnpm

@jason-ha
Copy link

jason-ha commented Feb 4, 2023

IDK why it happened

The solution is simple, replace %PNPM_HOME% with a normal path C:\Users\u\AppData\Local\pnpm

Prior to v7.17.1 setup created PNPM_HOME with special "expand" string type means it only has value after a certain point. Path is the same way. Path gets expanded just before PNPM_HOME is expanded because it is alphabetically before. Since PNPM_HOME doesn't have a value when Path is expended it is left with %PNPM_HOME%. If the initial letter in PNPM_HOME was anything less than P we'd likely have never noticed.

@AdvTechnoKing
Copy link

less than P

That's what I thought at first, but then I saw the %USERPROFILE% variable in %PATH% and thought it wasn't that simple.

@jason-ha
Copy link

jason-ha commented Feb 6, 2023

It is indeed not as a simple as alphabetical ordering, but the type of the value matters. USERPROFILE is not an expandable environment variable -- it has a simple string value.

C:\Users\Jason>reg query "HKCU\Volatile Environment" /v UserProfile

HKEY_CURRENT_USER\Volatile Environment
    UserProfile    REG_SZ    C:\Users\Jason

Notice REG_SZ is the type.
Compare to system with issue:

C:\Users\Jason>reg query "HKCU\Environment" /v PNPM_HOME

HKEY_CURRENT_USER\Environment
    PNPM_HOME    REG_EXPAND_SZ    C:\Users\Jason\AppData\Local\pnpm

You may fix your PNPM_HOME type by using:

reg add HKCU\Environment /v PNPM_HOME /t REG_SZ /d "%PNPM_HOME%" /f

which will keep its current value but overwrite the type.

I think this issue should be Closed now. It is fixed with 7.17.1's change. Rerunning setup won't fix things. So maybe it could be changed to patch the type.

@zkochan
Copy link
Member

zkochan commented Mar 12, 2023

This thread has too many unrelated issues. I am going to close it. If there are unsolved issues, create dedicated new issues.

Initially this issue was opened for CI envs. As a rule, it is not recommended to install packages globally in CI at all.

@zkochan zkochan closed this as completed Mar 12, 2023
@oshosh
Copy link

oshosh commented May 31, 2023

@cjosue15 Also solved on windows 10. thx

@mockingjet
Copy link

mockingjet commented Jun 27, 2023

I encountered the same issue too.

After I pnpm setup, still couldn't upgrade pnpm in my VSCodeterminal (Powershell).
I checked my system (Windows11) did have PNPM_HOME variable.
Then I opened another terminal window outside VSCode and upgraded successfully.
After that, I still needed to restart VSCode to use the latest pnpm.
I guess VSCode doesn't detect PATH variable changes or something.

@isimmons
Copy link

Same issue here on Windows 10 with it telling me the path is not in PATH. Updating to v8.6.8

Running the update command in a terminal outside VSCode gave me a clue what was happening. Not sure why it didn't tell me in VSCode but since I installed pnpm via npm i -g pnpm at some point long ago, the actual path to pnpm for me is C:\Users\isimm\AppData\Roaming\npm, not C:\Users\isimm\AppData\Local\pnpm so I had to manually fix PNPM_HOME to the right path in system settings.

Then reopen terminal inside or outside VSCode and it works.

I don't know why regular pnpm commands worked if the path was wrong. pnpm add worked in VSCode but pnpm list -g or pnpm i -g pnpm gave the error.

@juslin03
Copy link

@chioio thank you

@waldronmatt
Copy link

For some folks, simply opening a new terminal in vscode won't fix the issue after running pnpm setup. You might need to fully close out of vscode and restart which fixed the issue for me (windowss 11)

@DzmVasileusky
Copy link

If you installed it with npm you need to update it with npm as well. Since no global dirs were set up correctly
npm i -g pnpm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests