Fix NPM Error - EACCES: permission denied, mkdir

If you are encountering the "EACCES: permission denied, mkdir" error while running the NPM install command, don't worry, as this tutorial will guide you through resolving it. This is a permission error that typically presents itself when attempting to interact with packages via npm, such as during an npm install operation. This error is often caused by incorrectly using the sudo command while working with the node package manager (npm), which can create permissions issues between files and directories. Following the steps outlined in this tutorial, you can fix this issue and successfully install the necessary packages without further problems.

Using Sudo with NPM

Using sudo with the Node Package Manager (NPM) is strongly discouraged, and Node.js can and should be run without sudo permissions. This follows the security best practice of the Principle of Least Privilege (PoLP). Not only does following this security best practice help secure your system, but it also prevents permission issues that may occur due to the misuse of the sudo command, especially on the .npm directory. 

EACCES Error When Running NPM Install

Below is an example EACCES Error message I received when running npm install express inside a Node.js application where the .npm directory has permission issues due to misusing the sudo command in combination with the npm command.

  
    
npm ERR! code EEXIST
npm ERR! syscall mkdir
npm ERR! path /Users/peterg/.npm/_cacache/content-v2/sha512/ec/ea
npm ERR! errno EEXIST
npm ERR! Invalid response body while trying to fetch https://registry.npmjs.org/express: EACCES: permission denied, mkdir '/Users/peterg/.npm/_cacache/content-v2/sha512/ec/ea'
npm ERR! File exists: /Users/peterg/.npm/_cacache/content-v2/sha512/ec/ea
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.

npm ERR! A complete log of this run can be found in: /Users/peterg/.npm/_logs/2024-02-23T16_47_03_173Z-debug-0.log

  


Resolving the EACCES: Permission Denied Error

Let's assume you've misused the sudo command at some point when working with npm packages, and you receive this error as you attempt to install new packages using npm install. The first step to resolving this issue is ensuring the .npm directory has the correct ownership. Run the following command to change ownership of the .npm directory recursively.

sudo chown -R $(whoami) ~/.npm

Now, you can try an NPM package using the npm install command.

Still Facing Issues?

Unfortunately, if the above does not help you resolve your EACCES error, you may need to do one of the following:

  • Reinstall NPM

  • Change NPM's default Directory

NPM docs provides great documentation on how to accomplish these two bullet points. Make sure that whatever manual changes you make back up your install!

I hope this post was useful for you! Feel free to check out my other blog posts where I post content related to cybersecurity and other useful how to content.

If you have any questions or need some help feel free to hit me up on social media.

Previous
Previous

Expressjs: Organizing Routes and Routing for an API

Next
Next

Crypto Scams: Hacking Campaigns Compromise Coinbase Accounts