When you try to install an npm package globally using the npm install -g <package name>
command, you may find the following error:
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
The warning above means that your current terminal user doesn’t have “write access” to the /usr/local/lib/node_modules
folder.
Because you can’t write any new file and folder to the node_modules
folder, npm won’t be able to complete the installation.
You should see other errors below the warning as a consequence of the warning:
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/rimraf
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, ...
npm ERR!
npm ERR! The operation was rejected by your operating system.
Without the write access, npm will not be able to create folders and write the files for the package you are trying to install.
There are three ways to fix this error:
This tutorial will help you resolve the missing write access error.
Installing npm modules globally with the sudo command
You can run the npm install
command as root user by adding sudo
before the command:
sudo npm install -g @angular/cli
The sudo
command allows you to execute terminal command as the “root” user. You will be asked for your password when you invoke this command.
By calling the sudo
command, the error message with code EACCES should be resolved because root user has access to everything on your computer.
The drawback of this method is that you also need to add sudo
when you want to uninstall the package later.
As an alternative, you can change the owner of the node_modules
folder instead
Fix missing write access using chown command
The first way is to change the owner of the node_modules
folder, which must be owned by user “root” by default.
The following command will change the owner of the folder to your current user. I will explain the command below:
sudo chown -R $USER /usr/local/lib/node_modules
The chown
command is used to change the owner of the folder.
The -R
option means that the change owner command will be executed recursively, changing not only the node_modules
folder owner, but also the rest of the files and folders inside it.
Then, the $USER
is an environment variable that will be replaced with the current username you used to login to your computer.
Finally, the folder path /usr/local/lib/node_modules
is included to tell the terminal to change the owner of that folder.
By running the command above, you will be able to install npm packages again because the node_modules
folder now belongs to your current user.
But rather than running the command and changing the owner of the folder, I’d recommend you install NVM instead.
Fixing missing write access using NVM
NVM or Node Version Manager is a software that’s designed to be installed per-user basis.
It allows you to install multiple versions of NodeJS on your computer so that you can upgrade or downgrade your NodeJS version as needed.
The reason why using NVM would fix the missing write access command is that by default, NVM will install NodeJS versions to a folder under your current user.
For example, my NodeJS version is currently installed under the /Users/nsebhastian/
folder:
nvm which current
/Users/nsebhastian/.nvm/versions/node/v10.19.0/bin/node
When you install global packages using NVM, the package will be installed under the version’s lib/
folder:
/Users/nsebhastian/.nvm/versions/node/v10.19.0/lib/node_modules
By using NVM, you will have the ability to install different versions of NodeJS on your computer and you will automatically fix the missing write access error. I’m currently using it for my computer, and I’d recommend you to use it too 😉
You can learn how to install NVM here
Now you’ve learned how to resolve the permission denied error message caused by missing write access. Nice work! 👍