Skip to content

Instantly share code, notes, and snippets.

@dstokes
Created January 24, 2014 17:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstokes/8601766 to your computer and use it in GitHub Desktop.
Save dstokes/8601766 to your computer and use it in GitHub Desktop.
NPM failover in bash
# description:
# implement npmjs registry failover in your shell
#
# setup:
# npm install -g npm-delegate
#
# usage:
# npms install through
# npms i through async
#
npms() {
PORT=5983
REGLIST="http://registry.npmjs.org http://registry.nodejitsu.com http://registry.npmjs.eu"
(npm-delegate $REGLIST &)
# wait for the delegate server
while ! nc -vz localhost $PORT &> /dev/null ; do sleep 1; done
# shadow npm command with registry argument
npm --registry http://localhost:$PORT "$@"
kill $(ps aux | grep 'npm-delegate' | awk '{print $2}')
}
@soldair
Copy link

soldair commented Jan 30, 2014

i think im just going to go with trying all of them and using an install command

cd module && npmi

export NPM_REGS="http://registry.npmjs.org http://registry.nodejitsu.com http://registry.npmjs.eu http://npm.nodejs.org.au:5984"

npmi(){
  for reg in $NPM_REGS
  do
    npm install --registry $reg
    if [ "$?" -eq "0" ]; then
      echo "install from $reg successful!"  
      break  
    else
      echo "npm install from $reg failed. =("
    fi
  done
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment