How to globally install NodeJs packages without sudo in Linux and macOS

A lot of Linux and macOS newcomers to NodeJs got a problem with NPM installer: Either run npm install -g <package_name> under sudo privilege or install the package locally to the project (under node_modules folder) and call it via full path or npx. Fortunately, there’s a simple workaround that takes you less than a minute to solve the puzzle.

Just open your Terminal and make sure the location is your $HOME directory. If it isn’t, you’ll need to use the so familiar “cd” to change the directory to your $HOME folder. Then copy and paste this simple line to the current open Terminal and hit Enter or Return to start:

I’m not gonna dig into much detail here. The simple script helps you install NodeJs packages globally to your $HOME, or more exactly, $HOME/.npm-packages folder, so you can call the excutable files of your global packages anywhere in the Terminal. The only condition is they are only available in your user account, but that’s what we have been seeking for, right? If you don’t know how to source your bash, simply log out and log in back to your user account.

However, if you still can’t call your Node packages from Terminal, just open your .bashrc or .profile with Gedit or TextEdit, and paste these lines into the file, save and source it, or once again log out and log in.

That’s all. Everything must work in the exact way we expect. Tada! Enjoy!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.