corepack icon indicating copy to clipboard operation
corepack copied to clipboard

Permission issue on windows.

Open hyrious opened this issue 2 years ago • 17 comments

The node.js installer on windows (*.msi) will choose C:\Program Files\nodejs by default to place node.exe, npm and corepack. As you may know this place requires admin permission, the installer will bring up the UAC and ask for it once.

At the same time, npm (and pnpm) works well here because it will install packages to %appdata%\npm, where no permission is needed.

However, corepack will install pnpm to the same place as corepack by default (which is C:\Program Files\nodejs\node_modules\corepack. This causes permission issue:

> corepack enable
Internal Error: EPERM ....

A workaround is using --install-directory to manually change the install folder to some place without permission, for example %appdata%\corepack.

I hope corepack would use places like %appdata%\corepack on windows to avoid potential EPERM issues.

(BTW, the node.js installer for other systems are likely to choose a place with high permission. It is really an issue on macOS because it generally forbids installing anything globally, in which case users must use brew to correctly install it to a safe place. However I'm not expecting you to fix the installer logic and this is out of scope.)

hyrious avatar Nov 27 '21 15:11 hyrious

I am currently facing the issue. I had to run this command as administrator and it worked. However, now, I can't make yarn working, pnpm is working fine but if I run yarn commands it shows the below error. (same with admin rights)

Internal Error: EPERM: operation not permitted, rename 'C:\Users\arnab\.node\corepack\corepack-6616-5c3b9260.66dc1' -> 'C:\Users\arnab\.node\corepack\yarn\1.22.15'
Error: EPERM: operation not permitted, rename 'C:\Users\arnab\.node\corepack\corepack-6616-5c3b9260.66dc1' -> 'C:\Users\arnab\.node\corepack\yarn\1.22.15'

Do you think my issue is related to this one or I shall create another issue (and where, here or in yarn repo).

ArnabXD avatar Nov 28 '21 09:11 ArnabXD

This is causing problem as well, when using corepack with pnpm link --global. It produces the following issue:

pnpm link --global
 ERROR  The CLI has no write access to the pnpm home directory at C:\Users\username\AppData\Local\pnpm
For help, run: pnpm help link

louisgv avatar Mar 06 '22 16:03 louisgv

NVM, I was being a dummy. I purged that pnpm directory when trying to uninstall pnpm that I had gotten previously from their install scripts, but didn't remove its cache folders. After creating the pnpm folder in that location, everything works fine now

louisgv avatar Mar 07 '22 05:03 louisgv

Same as @ArnabXD Any updates on this issue ? Having the same problem (win10, nvm 1.1.8, node 16.13.0 selected). Tried to run git bash and cmd with admin rights. Tried to uncheck read only on node_modules, nvm folder, etc... without any success.

rikers avatar Apr 07 '22 15:04 rikers

@rikers Have you tried --install-directory somewhere?

From my personal experience, don't apply "run as admin" to any programming tool on Windows/macOS, it is likely to break everything. Instead find the correct way to put them at current user's scope (%appdata% is such place).

hyrious avatar Apr 07 '22 23:04 hyrious

Yup, just like described in your initial message. corepack disable for now, I'll try again later. Thank you anyway !

rikers avatar Apr 13 '22 12:04 rikers

Same issue here. Can't get past it

devgioele avatar May 04 '22 10:05 devgioele

I have the same issue, I am trying to upgrade the yarn from v1 to v3.

lewis-geek avatar May 23 '22 01:05 lewis-geek

I fixed the issue by installing the latest stable version node which is 16.15.0.

lewis-geek avatar May 23 '22 03:05 lewis-geek

I hope corepack would use places like %appdata%\corepack on windows to avoid potential EPERM issues.

I don't have a WIndows machine to test that, but my guess is that would indeed not trigger that EPERM error, but it also wouldn't make the scripts available on the path, so it wouldn't be much more useful. Can anyone tries this out? In any case, PRs are welcome.

aduh95 avatar Jul 23 '22 15:07 aduh95

but it also wouldn't make the scripts available on the path

Yes, you're right. But installing to the same place as node.exe would also annoy many users when they don't run in sudo mode (they shouldn't). In fact, I was considering %appdata%\npm too because this place is added to path by node.js installer and everything installed by npm i -g <pkg> would go there. But that may cause some conflicts with the npm.

The main problem here is corepack want itself to be treated as the same level as npm. It is half true in being bundled in node.js installers, but it does not have a global path (like %appdata%\npm for npm, set by installers too) to help doing its work. In which case the builtin corepack wouldn't be much more useful than npm i -g pnpm|yarn.

So here are some answers I guess to solve this issue:

  • Corepack re-uses the npm global dir (%appdata%\npm, you can get it by npm root -g) to install pnpm and generate executable shims (pnpm.cmd) there.

  • Ask the node.js installer to create %appdata%\corepack and add it to path and use that place on Windows by default.

Preknowledge: Permissions of Node.js on Windows

The installer installs node.exe and npm.cmd to C:\Program Files\nodejs. This place requires high permission which generally forbids any program that not run in admin mode to create and modify files, we can call it system scope. So how does npm install -g work? It is because npm installs packages to user scope (%appdata%\npm by default) where no permission is required.

  • npm i -g pnpmC:\Program Files\nodejs\npm.cmd i -g pnpm

    What happens: npm runs in user scope, Windows forbids it edit files in C:\Program Files\, it doesn't. npm creates some files in %appdata%\npm, like %appdata%\npm\pnpm.cmd.

  • pnpm i -g pkg%appdata%\npm\pnpm.cmd i -g pkg

    What happens: pnpm runs in user scope, pnpm creates some files in its global store (%localappdata%\pnpm by default, no permission is required).

hyrious avatar Jul 23 '22 16:07 hyrious

Do you know if it's actually %APPDATA% or %LOCALAPPDATA%? It seems to me that it would be more logical to go with the local one, but I'm not sure.

aduh95 avatar Jul 23 '22 20:07 aduh95

Although %localappdata% is perfered, in fact they are almost the same thing.

Let me explain the 2 folders quickly:

Windows has a tiny feature that it can sync program data automatically when it is put in %appdata%, but with some limitations: some file formats (.zip) don't get synced; total files' size bigger than some value will make the whole folder not get synced.

That's why you can see many programs occasionally using %appdata% and/or %localappdata% without fear. To some extent they are the same.

Nowadays programs won't rely on this place to sync settings, they should ask for storing data in OneDrive or require user login to save data to their database.

In fact, Windows is considering deprecate this feature in the future.

You can just treat them as 2 places that don't need admin permission to create and edit files. They are sort of ~/.local on linux.

hyrious avatar Jul 24 '22 00:07 hyrious

For me the issue was that the freshly installed Node.js installation folder had explicit permissions set instead of inheriting them. Right click -> Properties -> Security -> Advanced -> Change permissions -> Enable inheritance solved the problem.

AndreKR avatar Mar 30 '23 21:03 AndreKR

Having to set --install-directory every time as a workaround for this is quite tedious. Setting this via an environment variable in the shell (that could be set up by .zsh or .bashrc or similar) would be useful.

Would the maintainers be okay accepting a contribution adding an environment variable to this effect?

domdomegg avatar Aug 05 '23 23:08 domdomegg

I had the same issue. When I googled the error, it seemed like many people people were running "corepack enable" by running their terminal using "Run as administrator."

Not saying it's the right way to solve it, but it might be nice to have official guidance on this somewhere.

robmunger avatar Nov 16 '23 18:11 robmunger

Ask the node.js installer to create %appdata%\corepack and add it to path and use that place on Windows by default.

how can we do this? Did anyone solve this issue?

Rounak-stha avatar Mar 07 '24 06:03 Rounak-stha