How to Fix Missing Write Access Error When Using NPM

We use NPM whenever creating a Node.js application to install packages, update them, etc. But sometimes, you may get an error while doing these operations. The error message says “npm warn checkpermissions missing write access”.

The “missing write access” error can occur due to several reasons such as when you are trying to install packages globally, performing an operation where it required write access, or the operation trying to modify system-level files. 

There can also be some advanced reason for this, but without getting too into it, let’s straightforward jump to the section to see how you can fix this  “missing write access” error.

Also Read: A Beginner’s Guide to Node Package Manager

Fixing the NPM Missing Write Access Error

Let’s see the troubleshooting steps to resolve this error.

1. Using sudo or Run as Administrator

Suppose you are installing Express as a global dependency in your system, and you might get this error. Now you can use sudo in Linux or Mac to force the system to let this operation happen whether we don’t have proper write permission.

sudo npm install -g <package-name>

While if you are using Windows, you can run your command prompt or terminal as an administrate to get the operation done.

2. Change NPM Default Directory

We can assume that this error occurs because we don’t have write permission where we want to install the packages. So we can change the path of the global installation of npm modules to your user’s directory. No special permissions are needed to write to a directory owned by your user.

npm config set prefix ~/.npm-global
export PATH=~/.npm-global/bin:$PATH

This method can be complicated, so you should try this after you’ve tried all other methods and if they work, you don’t need to do this complex path redirection.

3. Using Node Version Manager (NVM)

We can use the NVM to avoid permission issues while installing npm packages. NVM automatically manages npm and the global package directory. You can install it in your system using the below command.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

After NVM is installed, you have to install specific versions of NodeJS by executing the below command.

nvm install <version>

You can install multiple node versions using this command.

Then you can use the below command to switch to a specific version of Node.

nvm use <version>

You can then use the traditional npm install -g <package-name> command to install packages globally. The packages will be installed in the relevant NVM-managed global package directory corresponding to the current Node.js version you are on.

4. Using Node Package Execute (NPX)

We usually install a package globally and then use it. The installation is causing permission issues. So we can avoid this installation step and use npx to directly use the package from the npm registry without installing it. 

npx <package-name>

Also, since npx comes with npm, you don’t need to do any setup or installation manually.

5. Fix Permissions

Last but not least, this method can fix “missing write access” for sure if none of the methods does.

We can execute the below command for this, just make sure that you have changed the username to your system username.

sudo chown -R <your-username> ~/.npm

This command will force the system to change ownership of the .npm folder in your home directory to the specified user. This method is not recommended until all the above steps can not fix the problem.

Summary

Whenever you are installing npm modules globally instead of installing them locally in the node_modules folder, a “missing write access” can occur. To solve this error, you can either install the packages using sudo or run as administrator or change npm’s default directory to your user. You can also use NVM or NPX. But if the error is still there, the straightforward method is to change the owner of the .npm folder to your current user.  We hope you find the answer you’re looking for. For more in-depth knowledge of NVM, check out: Run multiple Node.js apps with different Node.js versions in the same server.

Reference

https://stackoverflow.com/questions/33725639/npm-install-g-less-does-not-work-eacces-permission-denied

Aditya Gupta
Aditya Gupta
Articles: 161