Install Your Npm Packages Globally Without ‘sudo’ In 3 Steps

Adedunye Adedayo
4 min readJun 20, 2019

--

That Frustrating moment on my pc

Who else hates that scenario where you want to install an Npm package globally but you keep getting that annoying red text showing up on your terminal, all because of something called ‘permission error’. Well, that’s all about to change.

Why Permission Error

Permission error as the name implies only happens when you do not have sufficient access to anything be it a folder, a file, a function even boiling down to real life situation such as kissing someone else girlfriend in their presence (that’s a solid permission error).

By default when you install node, it sets up some kind of settings which I would not delve into fully. Part of these settings it that node needs to save globally installed files somewhere that it can access it anytime these files are needed. At the point of installing Node, this folder is set to somewhere inside another folder called usr/bin. This usr/bin is a folder that needs admin permission to allow writing and you whenever you are installing a module either globally or locally, you are actually doing a write operation. The only difference now is that when you install locally, you are writing to that same folder you are currently in but when you install globally, you are writing to that folder which is inside the ‘usr/bin’ folder and this folder needs admin permission to enable writing to it.

Why Sudo

Sudo comes to play where you prefix your command with sudo. For example

sudo npm install -g jest

After doing this, you are asked to input your password and this allows us to gain admin access to write into that folder. However, sudo is not a good practice as it could compromise security. Hackers could hack you through packages that you have given full access to.

Way To Go

This issue can be solved easily by changing the default folder for storing global files into another folder that doesn’t need admin permission before you can write to it.

It is usually recommended to back up your computer before doing this incase you do it the wrong way.

  1. On the command line in your home directory create a folder for global installations

mkdir ~/.npm-global

Explanation

The ~ signifies your home directory
The .npm-global is the name of the directory you are creating (whichever name you like)

In my case I already have the directory created. You notice that it replaced ~ with /Users/techkny. That’s my own home directory.

2. Configure Npm to point to the new directory created

Still on the command line

  • npm config set prefix '~/.npm-global'

Note: Don't forget that .npm-global is the name of the directory I created above

Note: You Could skip the remaining steps and proceed to step 6 which is a shorthand for step 3 -5. It is however advised to not skip them as you never can tell if it could break something in the future

Configure Npm

3. In your preferred text editor, open or create a ~/.profile file

Warning:

This is where I got it all messed up and I don’t want you to fall into the same mistake. Creating a ~/.profile file means creating a .profile file in your home directory.

Note the time created this morning

4. Open the .profile file you just created and add this line

export PATH=~/.npm-global/bin:$PATH

5. Update Your environmental variable to include the path you just specified.

Enter your terminal once again and put in this code

source ~/.profile

6. In case You get an error while trying to use the globally installed packages, you must have done step 3–5 wrongly. Then do the shorthand method. Add the path of the newly created directory to your environment variables

Put on your command line now

export PATH=~/.npm-global/bin:$PATH

And that’s it you are done. Comments and reviews are welcomed.

--

--