This script fixes the issues encountered when you are required to use 'sudo' to install npm global packages. The fix consists in creating a new local directory for global packages (default: ~/.npm-packages), configure node to use it and fix permissions. Existing global packages will be backed up and reinstalled after the fix.
The script is an improved version of npm-g_nosudo, based on the guide npm-global-without-sudo.
wget https://raw.githubusercontent.com/baxy/npm-global-no-sudo/master/npm-global-no-sudo.sh
or
curl -O https://raw.githubusercontent.com/baxy/npm-global-no-sudo/master/npm-global-no-sudo.sh
chmod +x npm-global-no-sudo.sh
./npm-global-no-sudo.sh
-
After updating your environment files, you will need to source the corresponding file before your npm binaries will be found in the current terminal session, e.g. for bash:
source ~/.bashrc
-
If NPM needs to be updated,
sudo
is still required:sudo npm install -g npm@latest
-
The script may cause issues if Node Version Manager is found installed. In this case it will exit and do nothing.
MIT © Adrian Sabau