How to Change The Global Modules Directory of Node.js in Ubuntu

Tutorial Jan 24, 2021

Node.js is an open-source, cross-platform JavaScript run-time environment for running JavaScript code outside of a web browser.

How to Install The Latest LTS Release of Node.js in Ubuntu

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, /usr/bin/, /usr/lib/ and /usr/share/.

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.

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 nano, vim or anything of your choice.

vim ~/.profile

Then add the following line to the end of the file.

PATH="$HOME/.npm-global/bin:$PATH"

Then run the following command to load the updated ~/.profile.

source ~/.profile

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.

Tags

Lime

Simple, like quantum physics.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.