microsoft / WSL

Issues found on WSL
https://docs.microsoft.com/windows/wsl
MIT License
17.56k stars 823 forks source link

npm package fails to install "npm ERR! Error: EACCES: permission denied" #14

Closed pallatee closed 8 years ago

pallatee commented 8 years ago

While trying to install a packge with npm (for example lodash) it fails with error message:

npm ERR! Linux 3.4.0+
npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install" "lodash"
npm ERR! node v5.10.1
npm ERR! npm  v3.8.3
npm ERR! path /root/test/node_modules/.staging/lodash-9a2aabe2
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall rename

npm ERR! Error: EACCES: permission denied, rename '/root/test/node_modules/.staging/lodash-9a2aabe2' -> '/root/test/node_modules/lodash'
npm ERR!     at destStatted (/usr/lib/node_modules/npm/lib/install/action/finalize.js:25:7)
npm ERR!     at FSReqWrap.oncomplete (fs.js:82:15)
npm ERR!
npm ERR! Error: EACCES: permission denied, rename '/root/test/node_modules/.staging/lodash-9a2aabe2' -> '/root/test/node_modules/lodash'
npm ERR!     at Error (native)
npm ERR!  [Error: EACCES: permission denied, rename '/root/test/node_modules/.staging/lodash-9a2aabe2' -> '/root/test/node_modules/lodash']
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! Please include the following file with any support request:
npm ERR!     /root/test/npm-debug.log

I have tried to install it both with and without starting bash in administrator mode.

vickerym commented 6 years ago

I was able to solve this by going in the /usr/local/lib/node_modules/npm, and giving myself read and write permissions there. Then the same thing in /users/ME/.npm/_locks. Not sure why i didn't already have permissions in the last instance.

npm 2.5.1

BenChirlinn commented 6 years ago

@vickerym are you running NVM or no? I have all those permissions I believe but still had the issue.

devzyrille commented 6 years ago

@joeRob2468 thanks a bunch! Worked like a charm!

brok3nSec commented 6 years ago

@vinifala thanks that worked for me!

codinguncut commented 6 years ago

I was not able to fix this problem with any of the above solutions (unsafe-perm, prefix, etc.). I do believe the issue is with open filehandles. I installed yarn and it worked straight away.

gnpkrish commented 6 years ago

If your workspace has package-lock.json please remove and try again to npm i. It might work, since i had a same issue in MS.

Thoughtscript commented 6 years ago

Fixed my install with NVM and NPM 8.8.0 using:

sudo chown -R u:p node_modules
sudo chown -R u:p u/.nvm

Where u is username and p is password. Had to do this on both directories! Thanks for the tips above, chmod wouldn't do.

Hope that helps someone!

PS - that was in the root directory of the project and root directory of WSL, respectively.

roshimon commented 6 years ago

@jgchristopher Thank You :+1: npm config set unsafe-perm=true worked for me :)

joelvarma commented 5 years ago

sudo chown -R myusername: /usr/local/lib/node_modules

This solved my issue

gujinyue1010 commented 5 years ago

@jgchristopher Thank You 👍 npm config set unsafe-perm=true worked for me :)

AsangiSathsarani commented 5 years ago

npm ERR! path E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules.staging\end-of-stream-07f5179b\index.js npm ERR! code EPERM npm ERR! errno -4048 npm ERR! syscall unlink npm ERR! Error: EPERM: operation not permitted, unlink 'E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules.staging\end-of-stream-07f5179b\index.js' npm ERR! { Error: EPERM: operation not permitted, unlink 'E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules.staging\end-of-stream-07f5179b\index.js' npm ERR! cause: npm ERR! { Error: EPERM: operation not permitted, unlink 'E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules.staging\end-of-stream-07f5179b\index.js' npm ERR! errno: -4048, npm ERR! code: 'EPERM', npm ERR! syscall: 'unlink', npm ERR! path: 'E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules\.staging\end-of-stream-07f5179b\index.js' }, npm ERR! stack: 'Error: EPERM: operation not permitted, unlink \'E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules\.staging\end-of-stream-07f5179b\index.js\'', npm ERR! errno: -4048, npm ERR! code: 'EPERM', npm ERR! syscall: 'unlink', npm ERR! path: 'E:\angulartemplate\coreui-free-angular-admin-template-master\coreui-free-angular-admin-template-master\node_modules\.staging\end-of-stream-07f5179b\index.js', npm ERR! parent: '@coreui/coreui-free-angular-admin-template' } npm ERR! npm ERR! The operation was rejected by your operating system. npm ERR! It's possible that the file was already in use (by a text editor or antivirus), npm ERR! or that you lack permissions to access it. npm ERR! npm ERR! If you believe this might be a permissions issue, please double-check the npm ERR! permissions of the file and its containing directories, or try running npm ERR! the command again as root/Administrator (though this is not recommended).

BernardMarieOnzo commented 5 years ago

npm config set unsafe-perm=true

MartinJHammer commented 5 years ago

In my case it was VS Code locking a file in node_modules. Closing VS code unlocked the file and made the error go away. npm config set unsafe-perm true, disabling antivirus etc. did not work.

fardhanardhi commented 5 years ago

If npm config set unsafe-perm=true isn't working Just try updating npm npm install -g npm

sudoJoe commented 5 years ago

It is not recommended to use sudo when installing npm packages.

Similarly, npm config set unsafe-perm true should be avoided.

Regardless of security concerns, neither option is necessary. A better solution:

  1. Open a terminal from your home directory and make a new directory for global installs: mkdir ~/.npm-global
  2. Configure npm to use the new directory path: npm config set prefix ~/.npm-global
  3. Open or create a ~/.profile file (I use vim, use whatever) and add the following line: export PATH=~/.npm-global/bin:$PATH
  4. After saving your ~/.profile ([Esc] -> :x! -> [Enter]), update system variables: source ~/.profile
  5. Test your new configuration by installing a package globally without using sudo: npm install -g pnpm

You can skip steps 2-4 if you prefer not to modify ~/profile by setting an ENV variable: NPM_CONFIG_PREFIX=~/.npm-global

Things work better for me when I set fewer environment variables.. YMMV.

Tested with:

Reference => official NPM docs:

parkerault commented 5 years ago

FWIW our team was having this problem intermittently trying to npm install on a mounted windows directory and were pulling our collective hair out re-installing node, checking versions, etc. It turns out it was just because we were all using vscode with the folder open in a workspace. You also need to make sure you don't have any running node processes (dev server, watching build script, etc). Once I shut all of those down I stopped getting the errors. I do suspect that there are many root causes of this issue though, and open file handles are just one of them.

fabiooliveira1 commented 5 years ago

Although, I'm using Ubuntu, I tried cleaning npm cache sudo npm cache clear --force

and then it allowed me to run without sudo npm i

The other options didn't work for me and I can't asure that this is the best option.

fuzzyTew commented 5 years ago

Fixed my install with NVM and NPM 8.8.0 using:

sudo chown -R u:p node_modules
sudo chown -R u:p u/.nvm

Where u is username and p is password. Had to do this on both directories! Thanks for the tips above, chmod wouldn't do.

Hope that helps someone!

PS - that was in the root directory of the project and root directory of WSL, respectively.

'p' should be the group, not the password. If you put that as your password, anybody can see your password by viewing the file stats !!!!!

fuzzyTew commented 5 years ago

I've successfully worked around this issue, letting for example cordova function correctly.

My fix is at https://github.com/fuzzyTew/nodejswslfix . The library must be compiled by hand and preloaded into npm's environment, and it intercepts the problematic call, providing a quick fix to this issue.

npm would be able to resolve this issue by altering their concurrent behavior when moving multiple submodules out of the staging folder. I haven't mentioned it to them or looked at the npm code, myself.

therealkenc commented 5 years ago

^--- Added some discussion in #1529 since it has the virtue of still being open.

sumitsumo commented 5 years ago

@borovaka you can fix that error by allowing unsafe perms

npm config set unsafe-perm=true

working ......thanks man

howyay commented 5 years ago

I tried every single solution posted in this issue but none of them worked, but I did find a way to make things work again...

For me personally I was just trying to update npm module so I downloaded the latest nodejs binary from their website and unpacked it on windows, then I did

sudo rm -rf /usr/lib/node_modules/npm

in subsystem and copied only npm binary in the archive to the same place using Windows Explorer, and finally I

sudo chmod a+x /usr/bin/npm

Now npm is up to date without updating nodejs along with it!

For anyone who tried other solutions here but didn't work :)

EDIT: to open Windows Explorer in current directory is explorer.exe . and you can navigate to root directory from there.

allain commented 5 years ago

If it helps anyone I've traced it down to having vscode-insider open while npm installing.

When it's turned off, it appears to work fine. It might have something to do with the watchers.

jonasdumas commented 5 years ago

This solved all my EACESS problem (on MAC):

sudo chown -R myusername: /Users/myusername/.npm/

g33kidd commented 5 years ago

@allain I can confirm this. Just shut down VSC Insider and my npm install worked fine.

AlexHayton commented 5 years ago

If it helps anyone I've traced it down to having vscode-insider open while npm installing.

When it's turned off, it appears to work fine. It might have something to do with the watchers.

Yep. Confirmed here too

soodakshay commented 5 years ago

This fixed my issue sudo chown -R $USER ~/.npm

Using MacOS Mojave

aruny121 commented 5 years ago

hey guys , if you using mac try running the command with sudo . it may help alot :)

ghost commented 5 years ago

This solved all my EACESS problem (on MAC):

sudo chown -R myusername: /Users/myusername/.npm/

Confirmed here too. Closed code-insiders and the error went away. Thanks so much.

beilber commented 5 years ago

If it helps anyone I've traced it down to having vscode-insider open while npm installing.

When it's turned off, it appears to work fine. It might have something to do with the watchers.

Same here as well. Win 10 WSL Ubuntu 18.04. Once I close code-insider it works like a charm.

hajimurtaza commented 5 years ago

This issue seems to come when changes are made to package.json file without npm knowing about it. Just do a plain npm install. Then install your module again. It should work. This is one of the scenarios that worked for me. There might be other issues causing similar problems.

1ycx commented 5 years ago

@hajimurtaza :

This issue seems to come when changes are made to package.json file without npm knowing about it. Just do a plain npm install. Then install your module again. It should work.

You're wrong there. This issue occurs when VSCode / VSCode Insiders is open in the project folder and you're trying an npm install.

fuzzyTew commented 5 years ago

@hajimurtaza : This issue seems to come when changes are made to package.json file without npm knowing about it. Just do a plain npm install. Then install your module again. It should work.

You're wrong there. This issue occurs when VSCode / VSCode Insiders is open in the project folder and you're trying an npm install.

There is no one specific scenario that is triggering this issue. It happens for different reasons for different people: from VSCode & other apps being open, from npm installation ordering details, or from permissions settings. Some problems can be resolved by closing or changing something, but some are deeply rooted in the issue.

hajimurtaza commented 5 years ago

@hajimurtaza : This issue seems to come when changes are made to package.json file without npm knowing about it. Just do a plain npm install. Then install your module again. It should work.

You're wrong there. This issue occurs when VSCode / VSCode Insiders is open in the project folder and you're trying an npm install.

There is no one specific scenario that is triggering this issue. It happens for different reasons for different people: from VSCode & other apps being open, from npm installation ordering details, or from permissions settings. Some problems can be resolved by closing or changing something, but some are deeply rooted in the issue.

Updated the answer.

EdwinRozario commented 5 years ago

I had node_modules installed before i installed nvm on the projet directory. Deleting them and running npm install worked for me.

clshortfuse commented 5 years ago

My issue was that npm existed on both my Windows installation and my WSL instance. When I uninstalled NodeJS from WSL, and ran npm, it would find and execute the Windows one. This leads me to believe that somewhere in the chain, the wrong npm was being called which doesn't really have access to certain parts of the WSL file system.

I solved it my issue by removing NodeJS from Windows. Alternatively, I could have removed %APPDATA%\Roaming\npm from the PATH system environment. Or simply, disabling AppendNtPath in the registry:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss]
"AppendNtPath"=dword:00000000
JordanMajd commented 5 years ago

This is a known issue with VSCode remoting and WSL (Microsoft/WSL#3395, Microsoft/WSL#1956) and should be fixed in WSL2.

This is caused because VSCode remoting watches files, which prevents them from being accessed elsewhere. The fix is to tell VSCode to poll when remote instead of watch. See below:

  1. ctrl+shift+p then type & select Preferences: Open Settings (JSON).
  2. Add "remote.WSL.fileWatcher.polling": true to the config
  3. ctrl+shift+p then type & select Developer: Reload Window.

For more information, see the documentation on VSCodes website regarding EACESS.

phurba123 commented 5 years ago

first run sudo su (as a root/administrator) and run npm install -g typescript as a root/administrator y.. worked for me

Cage-e commented 5 years ago

If you are on windows, re-installing IIS on my solved the problem for me.

clshortfuse commented 5 years ago

I've discovered it's an issue with open handles. VSCode can hold handles for longer than it should. Trying a command in just a bash shell open in Powershell can give EACESS errors. The act of just closing VSCode and trying the command again the bash shell works.

This has happened even when I close the related workspace on VSCode. The handle seems to only release when I actually close VSCode.

That doesn't make much sense, but perhaps the WSLv1 filesystem isn't properly closing files, or perhaps some logic faulty when a file handle is open and another tries to delete it?

mohitsud commented 5 years ago

It turns out it was just because we were all using vscode with the folder open in a workspace

This fixes it.

otaviopires commented 5 years ago

Got the same issue today running npm install -g gatsby-cli on WSL 2.

npm config set unsafe-perm true and npm config set unsafe-perm=true couldn't solve the problem.

I also didn't had my VSC open.

sudo chown -R $(whoami) ~/.npm and sudo chown -R $(whoami) /usr/lib/node_modules got me further but didn't solve the problem since gatsby wants to write into /usr/bin/gatsby.

Had to sudo it.

After following this this and this tutorials, I was able to install it without sudo.

Turns out it was just a matter of using nvm.

vamcs commented 5 years ago

Got the same issue today running npm install -g gatsby-cli on WSL 2.

npm config set unsafe-perm true and npm config set unsafe-perm=true couldn't solve the problem.

I also didn't had my VSC open.

sudo chown -R $(whoami) ~/.npm and sudo chown -R $(whoami) /usr/lib/node_modules got me further but didn't solve the problem since gatsby wants to write into /usr/bin/gatsby.

Had to sudo it.

After following this this and this tutorials, I was able to install it without sudo.

Turns out it was just a matter of using nvm.

Yes, you're installing it globally so you need to use sudo. NVM can indeed go around it since the packages are installed in your home folder instead.

dhavaljardosh commented 5 years ago

Using sudo worked.

sudo npm install -g npm@latest

szunaj13pl commented 5 years ago
# Remove nvm 
rm -rf $HOME/.nvm

# Install latest nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash

Latest install script can be found https://github.com/nvm-sh/nvm#install--update-script

iam3fun commented 5 years ago

I just ran WSL as admin and everything seems to be okay.....for now :) So now I am able to rum rm -rf npm install etc.

cheeers

tomahawk810 commented 5 years ago

It turns out it was just because we were all using vscode with the folder open in a workspace

This fixes it.

Yes. For me too. To be clearer: I closed VSCode. Instead of using VSCode integrated terminal, I used the Ubuntu 18.04 LTS app directly (in Windows, this opens a terminal).

crynut84 commented 5 years ago

@allain I can confirm this. Just shut down VSC Insider and my npm install worked fine.

great! thanks!!

Galzk commented 5 years ago

Figured you don't even have to close VSC, just make sure the directory is not currently open in the files list. Thanks for the tip!!!

CarlosFJ commented 5 years ago

npx [package] example to Ionic: npx ionic