Installing an NPM package globally is something that we all need to do at least once learning to use NodeJS. There is a high chance that you have encountered the missing write access to node_modules error before while performing this action.
That is why we prepared this article to save you valuable time and effort.
Fixing The Missing Write Access To node_modules Error With chown
The first method that you can use to fix this issue is by changing the node_modules folder’s owner. In most cases, this folder belongs to the “root” user as a default setting. You need to use the command below to change its ownership to the current user:
sudo chown -R $USER /usr/local/lib/node_modules
Now, we will be breaking down each component of the code.
sudo is there to allow you the capability to give out Terminal commands in the “root” role. Due to this nature, you should expect a prompt asking for your password after entering the code.
The next part is the chown command, which will change the folder’s ownership. There is also a -R option, which allows recursive operation for the ownership changing process.
As a result, it doesn’t just change the ownership for node_modules, but also all the sub-folders and files within that folder.
$USER is a placeholder variable, which should be replaced with your current username.
The last part is the folder path. You add it in to let the Terminal know of the folder’s location.
Once this command runs successfully, you can install the npm packages as the folder finally belongs to your user credential. This method is currently the most popular solution to this problem.
However, it does have some issues like requiring the folder’s exact location and a harder structure for newbies.
Fixing The Missing Write Access To node_modules Error With NVM
Node Version Manager (NVM) is a more approachable and simpler method, due to its software nature and its per-use installation design. You can put in various NodeJS versions on your computer, making it extremely easy to change the version when you need to.
As NVM installs the new NodeJS versions under your current user’s folder by default, it can easily fix this issue. After all, the nature of the missing write access to node_modules error is that of a permission problem. You are basically prevented from writing to the folder.
When you are using NVM, you are already installing things under your current user. As a result, there will be no check for permission to write into the folders.
This approach may appear to be more cumbersome at first. However, you will soon realize that it cuts off the problem from the source. You can save up quite a bit of time and effort if you work with node_modules on a regular basis. You don’t need to delete or clear variables.
With this article, we have shown you two approaches to solving the missing write access to node_modules error. Both solutions offer varying strengths and weaknesses. However, we do believe that the NVM method can end up more useful over a long time. You should check both of them out for yourself, though.
If you find this article helpful, look forward to our next release!