Skip to content

Instantly share code, notes, and snippets.

@barretts
Created November 17, 2015 03:32
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 barretts/0b0c3d0f366c85661697 to your computer and use it in GitHub Desktop.
Save barretts/0b0c3d0f366c85661697 to your computer and use it in GitHub Desktop.
npm install with retry on error from a Windows batch file
:: I created this increase CI/CD stability when running in cloud VMs; npm install failing is such a sad test failure
:: general batch loop thanks @AlexZhidkov https://github.com/FeodorFitsner/BedfordHarbourBOM/blob/master/nuget-restore.cmd
:: pipe npm, search log thanks @Ricky-Brno http://stackoverflow.com/a/22492458/604861
echo off
:: initiate the retry number
set retryNumber=0
set maxRetries=3
:RETRY
echo --------------------------------------
echo execute npm install
cmd /c "npm install > npm-install.log 2>&1"
for /f %%a in ('type npm-install.log ^| find /c /i "npm err!"') do set err=%%a
if "%err%" EQU "0" GOTO YAY
set /a retryNumber=%retryNumber%+1
echo error %err% was encountered during installation attempt %retryNumber%
if %retryNumber% LSS %maxRetries% (GOTO RETRY)
echo --------------------------------------
echo npm install failed (T_T)
echo --------------------------------------
EXIT /B 1
:YAY
echo --------------------------------------
echo npm install success (^^_^^)b
echo --------------------------------------
EXIT /B 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment