This is useful for avoiding permission errors like Error: EACCES: permission denied, mkdir
when installing an npm package without sudo, cause sudo npm i -g
looks off to me.
Script:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH
source ~/.profile
This will make a directory .npm-global for global installations.
Then npm is configured to use new directory path for global installations.
Then that PATH is exported, so you can call any globally installed module from terminal.
Then system variables are updated. You could have also logged out and logged back in, but this is faster.
Test: Download a package globally without using sudo:
npm install -g <somepackage>
To make the PATH export more permanent, edit `.profile` file and add:
export PATH=~/.npm-global/bin:$PATH
And do source .profile once again.
If you happen to have to source .profile on each login, check that your .bashrc contains:
if [ -f ~/.profile ]; then
source ~/.profile
fi