In this post, we will look at an easy fix for Missing Write Access error when we try to install some NPM package globally.

More often than not, this issue happens when we have setup NPM for the first time on any host machine and we try to install some package globally. It is mainly a permissions related issue and usually happens for Mac or Linux machines.

1 – The Error Message

The message itself might appear as below depending on the package in question.

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm WARN ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 but none is installed. You must install peer dependencies yourself.

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }

Basically, the issue here is that the user executing the command does not have write access to the folder /usr/local/lib/node_modules.

2 – The Fix for Missing Write Access

The fix for this issue is actually quite simple. We have to run the below command:

sudo chown -R $USER /usr/local/lib/node_modules

Let’s decode this command:

  • The sudo means we want to run this command as root i.e. the system super user. This is because we don’t have permission to write to the folder but the root user can fix any permission issues. While executing the command, you will be prompted for the password.
  • Next, we have chown. This command changes the ownership of a file or folder. The -R flag specifies that we want to change the ownership recursively. In other words, the owner access will be given to all the sub-folders and files.
  • $USER is an environment variable that is automatically set to your user id.
  • Finally, we have the folder path of which we wish to change the ownership.

After we run this command, our user will have ownership on the folder /usr/local/lib/node_modules.

Now, we can run the npm install command again to install the required package.

Important point to pay attention to the folder name in the error message. If it’s different, you need to update accordingly in the chown command.

Conclusion

And that’s about it.

Though the error looks quite complicated, it is actually very easy to solve it. We have seen a simple command to fix the issue and continue with installing the packages.

If you have any comments or queries, please feel free to write in the comments section below.

Categories: BlogNodeJS

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *