WSL: npm package fails to install "npm ERR! Error: EACCES: permission denied"
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.
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Reactions: 146
- Comments: 181 (7 by maintainers)
@borovaka you can fix that error by allowing unsafe perms
npm config set unsafe-perm=trueThis 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:
ctrl+shift+pthen type & selectPreferences: Open Settings (JSON)."remote.WSL.fileWatcher.polling": trueto the configctrl+shift+pthen type & selectDeveloper: Reload Window.For more information, see the documentation on VSCodes website regarding EACESS.
If you are using with -g option, try to remove it.
This seems also to be a valid issue when running npm in the new Windows 10 Bash
This solved it for me: https://gist.github.com/mattpodwysocki/4e6951fc93adae064a73ed0b2251d180
npm config set unsafe-perm trueI have no idea what it does however.
Closing VSCode before npm install solved the issue.
Same here. In fact if I try to update npm update -g npm@latest it attempts to copy lodash to the staging directory then throws the errror. Following this subsequent attempts to use npm are broken because it can no longer find lodash.
IMPORTANT
This really should be of utmost importance to the team. I for one was very excited and love how everything else works, but as you are adding this feature to windows with the intention of bringing web developers back to windows this needs fixing ASAP. This alone will stop me switching back to windows for web development and I’m sure it will be the same for many. It defeats the purpose of switching for me. Please please please fix.
Hey, I just managed to fix an issue with my setup by taking ownership of the folder that Node was installed to. I use NVM to manage node versions, so in my case Node was installed in
/home/myusername/.nvm.So, to fix it, I used
sudo chown -R myusername:myusername /home/myusername/.nvm. Weirdly enough, I had to run that command again each time I installed something globally, and I don’t know enough to figure out why. It’s a mystery!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.
FWIW our team was having this problem intermittently trying to
npm installon 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.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.sudo npm install -g npm@5.3.0that sorted it for meAnyone who got here with this issue whilst trying to run
npm iin VSCode remote WSL:Restart VSCode or close VSCode and run npm in your terminal. Seems to be a separate issue where VSCode is holding onto file references or something.
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).
This fixes it.
Hey quick fix! Instead of using npm install -g typescript use sudo npm install -g typescript Hope that helps. 😃
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?
Actually I had already set the
prefixto a local user directory, and I was still getting this error.To fully repro:
For anyone facing this issue: If you have mounted any windows folder in ubuntu so that you can use the command line functionalities and also GUI code editors through windows, this is bound to happen. Its because of some file locking rules done by windows.
So whenever this error comes up, close all the code editors or any other programs using this mounted folder in windows.
This has worked for me all the time.
How is this still an issue five years later? Microsoft, explain.
It is not recommended to use
sudowhen installing npm packages.Similarly,
npm config set unsafe-perm trueshould be avoided.Regardless of security concerns, neither option is necessary. A better solution:
mkdir ~/.npm-globalnpm config set prefix ~/.npm-globalexport PATH=~/.npm-global/bin:$PATHsource ~/.profilesudo:npm install -g pnpmYou can skip steps 2-4 if you prefer not to modify ~/profile by setting an ENV variable:
NPM_CONFIG_PREFIX=~/.npm-globalThings work better for me when I set fewer environment variables… YMMV.
Tested with:
Reference => official NPM docs:
You can bypass all permission problems by changing the prefix for npm. See option 2: https://docs.npmjs.com/getting-started/fixing-npm-permissions
The error still happens in
Windows 10 Creator's Update 10.0.15063. Here is what I tried:npm install -g npmcheck that npm was actually updated (>=4.4.4). Runhash -rto update bash cache.~/.profileis not executed in current BashOnWindows, so move all npm-relatedPATHmodifications from~/.profileto~/.bashrc.chowned by a user, I still runsudo npm install enduro -g.sudo npm install -g npmworked for meGet this error in 2020 (after openninig this issue in 2016) it still appearing. What the heck?! With this problem, I cannot to delete ./node_modules/ in my react project on last version of Windows LTSC. And VS Code does not support IntelliSense and autocomplete with error of access.
Ghost methods with
unsafe-perm=trueandsudonot worked. At least sudo not supported in Git Bash on Windows.And with old projects I have no this issue (created about half year ago). That feeling that problem in release of Create React App. But I can’t understand where is the problem to fix it by self.
Try to use sudo -S npm i
@allain I can confirm this. Just shut down VSC Insider and my npm install worked fine.
There is another wired thing about npm. When I try to install gulp globally I get:
As a new developer, this issue has had me stumped for days. In order to keep in-line with the rest of the team who use Linux or MacOS, I have to develop through WSL. This issue really needs to be solved.
For me, closing VSCode, opening WSL terminal and running npm install there worked.
hey guys , if you using mac try running the command with sudo . it may help alot 😃
Although, I’m using Ubuntu, I tried cleaning npm cache
sudo npm cache clear --forceand then it allowed me to run without sudo
npm iThe other options didn’t work for me and I can’t asure that this is the best option.
Same here with
Here’s the output
Still have the same issue, this is ridiculous! Opened in 2016 still not resolved.
This fixed my issue
sudo chown -R $USER ~/.npmUsing MacOS Mojave
Fixed my install with
NVMandNPM 8.8.0using:Where
uis username andpis password. Had to do this on both directories! Thanks for the tips above,chmodwouldn’t do.Hope that helps someone!
PS - that was in the root directory of the project and root directory of WSL, respectively.
I had the same problem, it has been fixed using nvm and wsl 2.
Before that I tried:
I followed all the steps and all worked fine for me. https://docs.microsoft.com/en-us/windows/nodejs/setup-on-wsl2
Note: I’m using Windows 10 Home Single Language
Same here as well. Win 10 WSL Ubuntu 18.04. Once I close code-insider it works like a charm.
sudo chown -R myusername: /usr/local/lib/node_modules
This solved my issue
I removed nodejs
sudo apt remove nodejsthen install nodejs current vestion from this link -> linkit worked for me.
I updated Windows to version 2004 today so I could use WSL2. I am now encountering this exact issue for the first time, having never been troubled by it in WSL1.
Windows 10 2004
WSL 2WSL 1 Ubuntu 20.04 LTS nvm v0.35.3 node v12.18.0 npm v6.14.4Edit: Apparently my distro was somehow still on WSL 1, even though it was installed after running
wsl --set-default-version 2. Apologies for the false alarm.Not sure what else to try but consistently running into this issue on Ubuntu 18.04 WSL 2 on 19033. Node v12.13.0, NPM v. 6.12.0. 1. Running
npm i -g npmwith npm installed via nvm to get away from global restrictions.$NVM_DIR, set in~/.bash_profilepointing to/home/myuser/.nvmand I’ve triedsudo chown -R $(whoami) $HOME/.nvm, cleaning the cache, and settingnpm config set unsafe-perm=truebut so far no luck. Previously I think I had installed node via curl (?) and it showed inusr/lib/but removed those directories and nowwhich npmandwhich nodeshows the correct nvm locations. Still no luck, even with VS Code closed and on repeat attempts. Error messages seem to always revolve aroundnode/versionnumber/lib/node_modules/.staging.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!!!
great! thanks!!
This whole two year thread is #1529. The problem here is not permissions and never has been. [It is utterly unrelated to #3 which was a symlink problem, and how this one got closed.] You can install your whole NodeJS ecosystem as user yourself; you are still going to trip #1529. I asked in a related post that WSL at least throw off a (nonconforming)
EBUSYso we could differentiate from actualEACESSproblems but there was no response (what you gonna do).Closing editors will help, if the thing holding open a handle is an editor. But if you take the example a couple of posts back,
.loader-fs-cacheis still holding a handle. No one is editing anything there. Y’all have been rebooting and discovering “works fine now”, because you’ve closed all the handles. And the thing is you’ve got open handles in node in spades much of the time, because of the client-server nature of the beast.For now you can (a) be meticulous about making sure there are no file handles open in your node universe before running
npm(b) hope that your node packages get updated to be more tolerant of the problem and/or © wait for #1529 to flip.Full disclosure here. Doing a little more testing on our internal build and finding that doing an “npm -g install <foo>” still throws the uv_pipe_open error, even when in ~/tmp.
We have someone looking into it.
If you installed node from apt-get, uninstall it completely and install it from NVM (Node Version Manager)
Uninstalling node (Ubuntu)
Update apt indexes
Install NVM
Finally, install node and npm
Using
sudoworked.sudo npm install -g npm@latestYou’re wrong there. This issue occurs when VSCode / VSCode Insiders is open in the project folder and you’re trying an
npm install.Yep. Confirmed here too
I just change my
npm@5.4.1tonpm@5.3.0, it’s fine now. You can usenpm i -g npm@5.3.0to downgrade.If you’re using VS Code and find yourself getting this error message, close VS Code, run the desired command, and restart VS Code, as indicated in https://stackoverflow.com/questions/60853873/eaccess-error-when-installing-packages-on-wsl
不行 如果用了sudo npm xxxx就会报npm找不到命令
Finally got a fix: ensured I did a full uninstall of any global (non-nvm) node/npm directories with
sudo apt-get purge --auto-remove nodejs(and whether this next bit was necessary or not) reinstalled my node version by doingnvm install [other_version],nvm uninstall [current_version], then reinstalled. Now working fine at the moment. Assuming there was something still trying to point to files in that original node install.npm adduser --registry=http://xx.xx.xx.xx then: npm publish --registry=http://xx.xx.xx.xx
well, I still have the issue.
sudo chown -R $(whoami) /usr/lib/node_modulesandsudo chown -R $(whoami) ~/.npmdidn’t worked.sudo npm i -g npmthrows this error:VSCode is closed, like every other node process via
killall node.NPM was installed via NVM and copied to
/usr/localvia:Installing any given node version via NVM works. Trying to update any NPM for the selected version gives the same error as above.
Tried aliasing the npm prefix - didn’t work as well.
If you are on windows, re-installing IIS on my solved the problem for me.
first run sudo su (as a root/administrator) and run npm install -g typescript as a root/administrator y… worked for me
Updated the answer.
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.
This solved all my EACESS problem (on MAC):
sudo chown -R myusername: /Users/myusername/.npm/npm config set unsafe-perm=true
@therealkenc can confirm it has to do with things being open elsewhere in your system, a pernicious and consistently annoying issue in Windows in general. At first I thought it was the ongoing node processes I had running from dev servers (I’d close them with task manager) but my latest test seems to indicate my text editor is the culprit, using Sublime Text 3. When I close it I can install modules normally.
Even adding
node_modulesdirectory to thefolder_exclude_patternandbinary_file_patternssettings I still have the issue somehow. Perhaps Sublime’s project implementation is touching the directories still? Not sure. Atom doesn’t seem to have the same issue (but I decided against using it on Windows since it apparently can’t do multi-line find replace. I just can’t win[dows]!).I’m running node 8.8.1 This is how I managed to get this to work:
CMD.exebashsudo npm install -g npm@5.3.0With the normal bash i could not get this to work.
This looks a lot like #3. We can keep the conversation going on there.
What I can say right now is that things are getting better (at least in non-mnt directories.). On my dev box I see:
It goes on from there. I should point out that I have not tried to actually use any of these, I have only installed them.
The team is hard at work getting stuff up and running. Npm is one of those things we’re looking at now.
Related: https://github.com/Microsoft/CommandLine-Documentation/issues/26
It seems to be an issue with symlinks
I have fixed my problem on mac by
sudo chown -R 501:20 "/Users/<my_username>/.npm"如果不用sudo就没有权限
I’ve come up with another possible solution
npm set unsafe-perm truehere is a linkI tried
npm cache clean --forceand then performnpm installand it works for meI’m still using https://github.com/microsoft/WSL/issues/14#issuecomment-481028594 to work around this reliably. Don’t use VSCode myself.
@ilanc - did you sudo your
npm install -g ...?On windows 10 I was trying to update dependencies in my project with
npm update --saveand it was returning permission errors. I tried to start the console in administrator mode but it wasn’t solving the problem.But with
npm config set unsafe-perm=trueit workedfollowing @vinifala suggestion (reverting npm v5.4.0 to 5.3.0) fixed my recent permissions issues on Windows 10 git bash.
npm install -g npm@5.3.0restart your bash. It seems there’s some permission timeout with forked(?) process.
This seems to be the underlying issue.
root@localhost:/usr/lib/node_modules/.staging# rename /usr/lib/node_modules/.staging/lodash-24c8c2f4 /usr/lib/node_modules/ember-cli/node_modules/babel-core/node_modules/lodash Bareword found where operator expected at (eval 1) line 1, near "24c8c2f4" (Missing operator before c8c2f4?) Unknown regexp modifier "/b" at (eval 1) line 1, at end of line syntax error at (eval 1) line 1, near "/."I needed nodejs@14 but npm@latest and using n to switch to node 14 and then npm i -g npm@latest always resulted in the error
To solve the issue I installed the latest node using n
n latestand thenexport N_PRESERVE_NPM=1and againn 14which allowed me to have node v14 with an up to date npm without this weird error你用的nvm安装对吧,我试过,我在root下也会报错
I was able to install a package by using sudo before npm and --force at the end.
Example: sudo npm install [package name] --save-dev --force
不懂,太邪乎了
+1 it fixed the same issue for me after moving from dirictly installed nodejs to nodejs intalled from nvm. Thank you very much! You saved my hours!
I’ve tried many other solutions and none of them has worked! This one did!!! Thanks
So I have to work on a machine with WSL v1 (no issues on v2). I can replicate it pretty easily by trying to install a package with VSCode. I believe there’s some sort of access limit or open handles.
Basically, I know VSCode is monitoring the folder I’m working in. If I install a package with VSCode is open, I’ll get EACCESS errors. If I close VSCode and install the packages over the Ubuntu bash, no errors.
So, to create this issue, I believe you just have to:
npm install)I would think VSCode isn’t handling the file system changes faster than
npmcan create or delete them. That’s why this only happens sometimes and on some machines. The machine I’m working on currently is REALLY slow in general and I get this errors consistently if I try it installing a package with VSCode open. WSLv2 may have fixed this issue, or it’s just so much faster than WSLv1 that the it’s rare for the collision to occur.I’m still having the problem even though I closed VSCode and used:
sudo npm install -g npm@latestGot the same issue today running
npm install -g gatsby-clion WSL 2.npm config set unsafe-perm trueandnpm config set unsafe-perm=truecouldn’t solve the problem.I also didn’t had my VSC open.
sudo chown -R $(whoami) ~/.npmandsudo chown -R $(whoami) /usr/lib/node_modulesgot 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.
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.
^— Added some discussion in #1529 since it has the virtue of still being open.
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.
If your workspace has
package-lock.jsonplease remove and try again tonpm i. It might work, since i had a same issue in MS.@joeRob2468 thanks a bunch! Worked like a charm!
I fixed this issue using npm install -g npm@next I am working on a Windows 7 pro
My anti-virus was blocking the download of packages. Thanks ilyaigpetrov.
I just ran into this trying to do
npm install -g ember-cli/ember-clion 15063.2. 😕npm config set unsafe-perm=truefixed it, but makes me uneasy.