How to fix missing write access to node_modules folder

Here's how you can fix the missing write access error to node_modules folder

Posted on April 10, 2021


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

This error usually happens on a computer with either Mac OS or Linux operating system. It means that your current Terminal user doesn’t have a “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.

There are two ways to fix this error, and this tutorial will explain both ways for you. The first is through changing the owner of the folder.

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

First, 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.

Next, 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 variable is a placeholder 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

See also:

Level up your programming skills

I'm sending out an email once every Wednesday with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.