Skip to content

Instantly share code, notes, and snippets.

@niksmac
Last active January 9, 2023 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niksmac/6c81a7697305c2a466215b4f2110d7bf to your computer and use it in GitHub Desktop.
Save niksmac/6c81a7697305c2a466215b4f2110d7bf to your computer and use it in GitHub Desktop.
Fixing npm permissions
#!/usr/bin/env bash
set -e
echo ''
info () {
printf "\r [ \033[00;34m..\033[0m ] $1\n"
}
user () {
printf "\r [ \033[0;33m??\033[0m ] $1\n"
}
success () {
printf "\r\033[2K [ \033[00;32mOK\033[0m ] $1\n"
}
fail () {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] $1\n"
echo ''
exit
}
if test ! $(which npm)
then
fail "npm not found, exiting."
exit 0
fi
# Make a directory for global installations:
info 'Make a directory for global installations:'
mkdir ~/.npm-global
# Configure npm to use the new directory path:
info 'Configure npm to use the new directory path:'
npm config set prefix '~/.npm-global'
# Making YOU the owner
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
# Open or create a ~/.profile file and add this line:
info 'Open or create a ~/.profile file and add this line:'
export PATH=~/.npm-global/bin:$PATH
# Back on the command line, update your system variables:
info 'Back on the command line, update your system variables:'
source ~/.bashrc
success 'Everything set up 👍'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment