Resolving EACCES permissions errors when installing packages using NPM

Pavan Skipo
Junior Dev
Published in
2 min readApr 19, 2020
Photo by Mikhail Vasilyev on Unsplash

As with any problem there are multiple ways to fix this too, but first lets look at how not to fix this.

Do not fix this problem by giving sudo permission or by changing the ownership of system directories !

What am I talking about ?

I’m talking about commands like these

$ sudo npm install$ sudo chown -R $USER~/.npm$ sudo chown -R $USER/usr/local/lib/node_modules

which are very tempting to use when we are in a hurry and just want to get it working, but doing so will be expensive later on.

Why sudoeing/sudoing with npm is a bad idea ?

  • When we install a package globally by using sudo npm install -g it will create files like ~/.npm/_locks which causes the EACCES error.
  • Malicious packages can be installed using npm and if it was installed using sudo then that means that the Malicious script has Admin access ( It’s like giving Thanos the Infinity gauntlet…).
  • Sometimes even if a package is not malicious there might be some scripts that can cause problems unintentionally.
  • Giving sudo npm install will create a node_modules directory with root privilege, so ever other npm command will also require sudo.

How to fix this problem the right way?

On a development machine NVM provides many advantages such as installing different versions of node and installing node without sudo. First uninstall the node from the system, then :

  1. Install NVM ( here is detailed instruction based on OS )
  2. Then run nvm install node

Now npm <command> will no longer require sudo privilege.

…and so all the projects in the system we at peace as they no longer were fighting for privileges and they lived happily ever after, THE END.

--

--

Pavan Skipo
Junior Dev

Software Engineer by Day | Game Developer by Night. If you want to know more about me visit: https://pavanskipo.com/