Windows批处理安装脚本执行npm后停滞,需重复运行问题求助
解决批处理脚本npm install后后续步骤无法执行的问题
问题描述
批处理脚本执行完npm install后,后续的返回主目录、删除config.json、执行python init.py等步骤无法自动运行,必须手动执行两次脚本才能完成全部安装流程。尝试过添加超时等待循环,但没有效果。
核心原因
npm install在部分场景下会启动子进程完成依赖安装,直接运行npm install时,批处理会在主npm进程退出后就继续执行后续代码,但此时子进程可能还在创建node_modules目录或处理依赖,导致后续步骤因文件/目录状态异常而失败;第二次运行时node_modules已经存在,跳过npm安装步骤,所以流程能正常走完。
解决方案
1. 强制等待npm进程完全结束
用start /wait命令运行npm install,确保批处理等待整个npm安装流程(包括所有子进程)完成后再继续:
echo Installing Node.js dependencies... start /wait npm install --no-fund
2. 校验npm安装的退出码
在npm install后检查错误码,确保安装成功再执行后续步骤,避免异常情况下继续流程:
npm install --no-fund if %ERRORLEVEL% NEQ 0 ( echo Node.js dependencies installation failed. Aborting... exit /b 1 )
3. 确保目录切换可靠
给cd /d命令添加失败处理,避免因目录不存在导致后续步骤在错误路径执行:
cd /d "%repo_dir%\Warframe-Algo-Trader\my-app" || ( echo Failed to access directory: %repo_dir%\Warframe-Algo-Trader\my-app exit /b 1 )
修改后的完整脚本
@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%" || ( echo Failed to access directory: %repo_dir% exit /b 1 ) 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 if %ERRORLEVEL% NEQ 0 ( echo Git clone failed. Aborting... exit /b 1 ) ) REM Install Python dependencies pip install -r "%repo_dir%\Warframe-Algo-Trader\requirements.txt" if %ERRORLEVEL% NEQ 0 ( echo Python dependencies installation failed. Aborting... exit /b 1 ) pip install uvicorn if %ERRORLEVEL% NEQ 0 ( echo Failed to install uvicorn. Aborting... exit /b 1 ) REM Set the installation directory to the 'my-app' folder cd /d "%repo_dir%\Warframe-Algo-Trader\my-app" || ( echo Failed to access directory: %repo_dir%\Warframe-Algo-Trader\my-app exit /b 1 ) 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... start /wait npm install --no-fund if %ERRORLEVEL% NEQ 0 ( echo Node.js dependencies installation failed. Aborting... exit /b 1 ) ) REM Go back to the main 'Warframe-Algo-Trader' folder cd /d "%repo_dir%\Warframe-Algo-Trader" || ( echo Failed to access directory: %repo_dir%\Warframe-Algo-Trader exit /b 1 ) 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 if %ERRORLEVEL% NEQ 0 ( echo Initialization failed. Please check the error messages above. exit /b 1 )
内容的提问来源于stack exchange,提问作者zfa- 2
相关产品推荐
相关产品推荐

