Fixing npm EACCESS errors
(permission denied, mkdir)
Build tools like Gulp, Grunt and Webpack are pretty amazing - but sometimes when I am using npm to install the node modules required for my project I run into issues.
Recently when running the basic
npm install command in the terminal I've been getting quite a few errors along the lines of:
Error: EACCES: permission denied, mkdir
The first thing I tried was running it with sudo.
$ sudo npm install
I checked I was running the latest versions of node and npm, and updated to the latest stable release just in case.
$ sudo npm update
Then I deleted the existing node modules folder in my project and tried running the command again. But I kept getting the same errors for those packages, which was really frustrating.
I did some googling and found adding a particular command worked for quite a few people. So I tried:
$ sudo npm install --unsafe-perm=true --allow-root
Unfortunately, it didn't work for me.
I went on for more googling and came across this npm community thread where one poster suggested we need to change the permissions of the folder. The code he suggested was:
$ sudo chown -R $(whoami) ~/.npm
$ sudo chown -R $(whoami) /usr/local/lib
$ sudo chown -R $(whoami) /usr/local/bin
And it worked, everything installed correctly after that!
I have a basic understanding of how this command updates the folder permissions thanks to a mini command-line course I took a couple years ago, but I wouldn't have gotten there on my own.
I'm one happy chappy and can continue on with the React Gatsby project I was working on. I just wanted to document this for future me next time I come across this issue!