Peter Girnus

View Original

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.

See this content in the original post


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.

See this content in the original post

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.

See this social icon list in the original post