Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.