node.jsbatch-filenpm-scripts

My installer script for a program stalls after installing node.js's npm and doesnt continue


I have an installation script that installers a specific github repo and all of its requirements.

this is the installer snipit that seems to be inconsistent:

@echo off

REM Check if Python 3.11 is installed
python --version 2>&1 | findstr /I "3.11"
if %ERRORLEVEL% NEQ 0 (
    echo Python 3.11 is not installed. Please install Python 3.11 from the official website.
    exit /b 1
)

REM Check if Git is installed
where git > nul 2>&1
if %ERRORLEVEL% NEQ 0 (
    echo Git is not installed. Installing Git...
    powershell -Command "Start-Process https://git-scm.com/download/win -Wait"
)

REM Check if Node.js and npm are installed
node --version 2>&1 | findstr /I "v"
if %ERRORLEVEL% NEQ 0 (
    echo Node.js is not installed. Please install Node.js with npm from the official website.
    exit /b 1
)

REM Check if Python requests library is installed
pip show requests | findstr /I "Name: requests"
if %ERRORLEVEL% NEQ 0 (
    echo Installing requests...
    pip install requests
)

REM Set the installation directory to the user's home directory
set "repo_dir=%USERPROFILE%\algorithm-trader-warframe"
mkdir "%repo_dir%" 2>nul
cd /d "%repo_dir%"

REM Clone the Git repository into the specified directory
if exist "%repo_dir%\Warframe-Algo-Trader" (
    echo Repository is already cloned in %repo_dir%\Warframe-Algo-Trader.
) else (
    echo Cloning the Git repository into %repo_dir%\Warframe-Algo-Trader...
    git clone https://github.com/akmayer/Warframe-Algo-Trader
)

REM Install Python dependencies
pip install -r "%repo_dir%\Warframe-Algo-Trader\requirements.txt"
pip install uvicorn

REM Set the installation directory to the 'my-app' folder
cd /d "%repo_dir%\Warframe-Algo-Trader\my-app"

REM Check if Node.js dependencies are already installed
if exist "node_modules" (
    echo Node.js dependencies are already installed.
) else (
    echo Installing Node.js dependencies...
    npm install --no-fund

    REM Wait for npm installation to complete
    :WAIT_NPM_INSTALL
    if not exist "node_modules" (
        timeout /t 5 /nobreak > nul
        goto WAIT_NPM_INSTALL
    )
)

REM Go back to the main 'Warframe-Algo-Trader' folder
cd /d "%repo_dir%\Warframe-Algo-Trader"

REM Remove the existing config.json file (if it exists)
if exist "config.json" del "config.json"

REM Initialize the tables and create a new config.json file
python init.py

The issue consistently happens where it doesnt proceed with python init.py and the other scripts. a consistent solution is running the script twice. now i have a theory its probably due to improper timing but besides that i cant find anything online or through GPT that suggests the cause of the issue and its solution.

Ive already tried adding timeouts and await loops but it doesnt not seem to work. The script always works on the second time its run and i would like to minimize that to only once without stitching things together.


Solution

  • Hmm.. first issue for regular batchers is the unmentioned matter that Mingw is in operation.

    Mingw overrides the normal operation of some standard batch utilities, so needs to be mentioned.

    I believe that this is not an issue here.

    First matter: The cause of "it works the second time around" is normally that the standard opening of a batch file

    @echo off
    setlocal
    

    has not been followed. This discards any changes made to the environment when the batch ends, so variables established by one batch file do not affect any further batches that may be run in the same session.

    The conclusion therefore is that an environment variable is being established by the first run, not deleted when the first run ends, and is then used in the second run.

    Hence - there is most probably a set statement being executed in the first run which establishes a variable used in an earlier step. This may be explicit (the variable is mentioned by name in the script) or implicit (the variable is used by a subscript or utility). Or it may be that a directory is established in the first run that is assumed to exist at some point, or the current directory is assumed to be different from its actual location.

    So the candidates are

    repo_dir
    

    or some variable established by a subscript OR the current directory at the start, since there appear to be cd instructions that are not being undone (setlocal restores the original directory when the batchh ends)

    So - I'd advise setting up repo_dir at the start of the script and then creating the required subdirectory/ies.

    Next problem is it doesnt proceed with python init.py and the other scripts. Since python init.py is the very last line, I'd deem the other scripts to be the problem - but I don't know what they are. Perhaps running them would depend on having the correct directory as current, perhaps dependent on repo_dir.

    Finally,

    REM Check if Node.js dependencies are already installed
    if exist "node_modules" (
        echo Node.js dependencies are already installed.
    ) else (
        echo Installing Node.js dependencies...
        npm install --no-fund
    
        REM Wait for npm installation to complete
        :WAIT_NPM_INSTALL
        if not exist "node_modules" (
            timeout /t 5 /nobreak > nul
            goto WAIT_NPM_INSTALL
        )
    )
    

    is not going to work. A code block (parenthesised sequence of statements) cannot contain a label.

    REM Check if Node.js dependencies are already installed
    if exist "node_modules" (
        echo Node.js dependencies are already installed.
        goto nodejsinstalled
    )
    echo Installing Node.js dependencies...
    npm install --no-fund
    
    REM Wait for npm installation to complete
    :WAIT_NPM_INSTALL
    if not exist "node_modules" (
      timeout /t 5 /nobreak > nul
      goto WAIT_NPM_INSTALL
    )
    
    :nodejsinstalled
    

    Is more likely to work