Skip to content

Instantly share code, notes, and snippets.

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
:: pipe npm, search log thanks @Ricky-Brno
echo off
:: initiate the retry number
set retryNumber=0
set maxRetries=3
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 --------------------------------------
echo --------------------------------------
echo npm install success (^^_^^)b
echo --------------------------------------
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.