npm -g install <npm_package>not working as desired. Why ?? Why ?? Why??

Jagatjyoti G Tuladhar
3 min readJan 20, 2018

--

tldr;

Y ou installed node js latest version from nodejs official site and with it npm too comes along. Tested the node and npm is working correctly with commands like

node -- version
v8.9.4
npm -- version
5.6.0

But please don’t get into the delusion that your npm is working all good and correctly. Because that’s what is was thinking about.

Problem

OK, so whats the problem here. The problem can be with your global npm installation of the package. While all things are working just fine with running local project with its dependencies, there might be the problem with global installation of packages which might not get installed or if installed cannot be accessed in terminal bash command. And its a headache to solve. For me it was. Because first thing you google about this problem it it will say reinstall the npm properly.

#installing package globally
npm install -g lite-server
lite-server app.js
#unexpected output
lite-server: command not found

The problem is mainly due to two reasons: (#as i have identified#haha)

  1. The npm is installed in the root directory and you need super user privileges to use it. This is easier problem to solve. Trust me!!!
sudo npm install -g <npm_package> #this will solve your problem

2. Even though you have installed npm with standard user privileges, (you can check it, if you don’t get any error while installing npm -globally) but still not able to access global packages you are quite in a stuck.

Solution

Step 1: Check the npm package is really installed globally.

npm list -g --depth=0

This will show your npm package list which are installed globally. If it then ALRIGHT we are in correct steps HAHA

Step 2: Next check the npm globally save path.

npm bin -g#output most probably but even if not don't worry till now
/home/<user>/node_modules/.bin

And its all good but why i’m not able to access???????????

Step 3: Change the permission of node global package. Here we won’t be tampering with default file permission rather change the directory itself with creating a new profile.

mkdir ~/.npm-global
npm config set prefix `~/.npm-global`
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
npm install -g <npm_packages>

This is one of the recommended things done by npm official itself ie if you don’t trust me.

Step 4: All these above steps are done and you will find it all the forum and blogs easily. But still it doesn’t work.

So still why there is the problem. Let me take a minute to make you understand. For any command to work in bash easily. There must be a script file or path defined in /bin folder to be able to access. If it is not there how can your system know this is the path to search. Common sense it’s it.

The simple way is to make a syslink of the going to ~/.npm-global/<package> but every time is not a good fix here. So here is the final step to solve it.

Step 5: Go to .profile file in the HOME_DIRECTORY and change it yourself manually.

gedit ./.profile

Add the following lines manually.

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

It was done in above steps too but if it wasn’t saved in .profile file you have to do it manually.

Restart the system as you are changing the system config file and bash doesn’t take effect without it.

And if It it works buy me a beer LOL.

--

--