Error: EACCES and Ruined Plans
I was going to write this about something else but then I tried to install something and hit the wonderful “npm ERR! Error: EACCES: permission denied”. So now I am just going to work to solve this stupid thing since it’s been annoying me anyway. This is for a WSL machine, by the by.
It seems the root of this problem results from being cavalier with sudo installing things. I didn’t think I did this, but will take the internet’s word if it puts the blame for something not working at mine own feet. So unless you install npm as the root user, it will lock out every user other than root, thus forcing you to sudo install everything. Then if you are trying to install an npm with post-install scripts, it won’t let you install it, and if you ignore the scripts, you are left with a mucked-up install and a niggling in the back of your mind: Is this broken because I’m doing something wrong or because the install is broken? Here is the advice I set out to follow: https://hackersandslackers.com/fixing-your-npm-installation/
The advice is to uninstall Node then install it again. I was supposed to run
apt remove node
then
apt remove nodejs
Well, no dice, off to a bad start:
E: Could not open lock file /var/lib/dpkg/lock-frontend — open (13: Permission denied)
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
I was then to run
which node
to see if the removal was successful. Well, since it wasn’t, I tried deleting the folder that was returned. In order to find it I had to go my user/AppData/Local/Packages/CanonicalGroupLimited.UnbuntuonWindow…/LocalState/rootfs/usr/local/bin
How easy is that?
Okay, then let’s install node:
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
apt-get install -y nodejs
First line of code went off without a hitch, the second failed.
Could not open lock file /var/lib/dpkg/lock-frontend — open (13: Permission denied)
Alright, cool, it’s 2 am and I’m starting to get frustrated, so we’ll sudo it up, the thing that caused all these problems in the first place! Alright that worked, let’s update NPM:
npm i -g npm@latest
That worked too! Things are going great, nothing is going to go wrong, I’m not writing this with hindsight. The next step was to make a Global NPM directory, so I ran these:
1. nvm use — delete-prefix v15.3.0 — silent
2. npm config set prefix ‘~/.npm-global’
3. echo ‘export PATH=~/.npm-global/bin:$PATH’ >> ~/.bashrc
4. source ~/.bashrc
Alright, we did all that. Let’s try to install that npm that started this all annnnnnd…
npm ERR! Error: EACCES: permission denied
Ughh. Okay, back to Google. Looks like I can try to perma give my WSL login root access or if I run:
sudo -i
that gives command-line root access. I do everything outlined again and at the end of all that, it works! My npm installed without Access problems! To further prevent any mishaps, it’s recommended that if you’re hitting the error in VSCode, close out all instances of VSCode and do whatever was giving you permissions problems in your normal terminal. The extra layer of VSCode seems to cause additional issues when renaming or adding files as you are trying to reconcile the files somewhat existing in VSCode then also on your hard drive.
There we have it, it’s now 3 am but I didn’t let it beat me, we fixed the problem and with the creation of the directory we will hopefully never have this issue again. Or I further broke things and there will be a blog post about fixing everything I just did. Fun!