NPM is the default package manager that comes with Node.js. And by default the location where packages get downloaded to is
/usr. Some of the directories that gets used by Node.js are,
The reason to change the location of the global modules directory can be as simple as to avoid using
sudo for installing modules globally. Or just for convenience. In any case, the process is simple.
To change the global modules directory to somewhere else, we will use the
config command of
npm config set prefix ~/.npm-global
The above command changes (sets) the global modules directory to
.npm-global located in the current user's home directory.
Next, we need to add the
.npm-global/bin directory to the PATH variable. For that, open up
.profile in a text editor like
vim or anything of your choice.
Then add the following line to the end of the file.
Then run the following command to load the updated
Note: If you are using Ubuntu in a desktop environment, a log-off and log back in will be required to make the above action persist onto to newly opened terminal windows.
And that is all. We successfully changed the global modules directory used by NPM. You no longer have to use
sudo to install modules globally.