Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
THE MULTI-PURPOSE NPM TROUBLESHOOTING SCRIPT
#!/bin/bash
# THE MULTI-PURPOSE NPM TROUBLESHOOTING SCRIPT
# Guaranteed to fix any and every problem with your npm install!**
#
# © ️Ian Young 2016
#
# Usage:
# Simply run the script, passing as arguments the command that is failing.
# Come back in 1-45 minutes to a totally fixed npm install.
#
# Examples:
#
# $ npm-troubleshooter npm install
#
# $ npm-troubleshooter grunt build
NPM_COMMANDS[0]='npm install'
NPM_COMMANDS[1]='rm -rf ./node_modules'
NPM_COMMANDS[2]='npm shrinkwrap'
NPM_COMMANDS[3]='rm npm-shrinkwrap.json'
NPM_COMMANDS[4]='npm rebuild'
NPM_COMMANDS[5]='npm prune'
test_command=$@
last_return=1
while [ "$last_return" -ne "0" ]; do
len="${#NPM_COMMANDS[@]}"
rand=$(( RANDOM % len ))
echo "Running \`${NPM_COMMANDS[$rand]}\`"
${NPM_COMMANDS[$rand]}
echo "Testing for success…"
$test_command
last_return=$?
done
echo "Success! Your npm is fixed. Until next time, my friend."
# ** Not actually guaranteed in any form. In fact, while we're on the subject:
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# In case you haven't figured it out: yes, this script is written tongue-in-cheek.
# You probably shouldn't use it to fix your npm install, or run it at all for that matter.
# I am not reponsible for any bad things that happen to your files if you do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.