批处理文件调用 Node.js - 如何从 Node 设置 ERRORLEVEL

Dou*_*oug 6 batch-file errorlevel node.js

我有一个调用节点程序的批处理文件:

@echo off

echo Starting Deploy.js: %date% %time%
echo CurrentDir: %CD%

node .deploy/deploy.js
IF %ERRORLEVEL% NEQ 0 goto error

echo Finished Deploy.js: %date% %time%

echo %~n0: Completed %date% %time%

goto end

:error
endlocal
echo An error has occurred during web site deployment.
call :exitSetErrorLevel
call :exitFromFunction 2>nul

:exitSetErrorLevel
exit /b 1

:exitFromFunction
()

:end
endlocal
Run Code Online (Sandbox Code Playgroud)

I'd like to be able to have my node.js program be able to have it hit the error condition on failure of the application.

How do I trigger that in node.js?

I tried having my main function return 0 or 1, but it's not automatically setting the ERRORLEVEL.

What is the proper way to do this?

npo*_*aka 4

检查有关 process object 的node.js 文档。在您的情况下,您不需要回调函数,因此您可以简单地使用:

process.exit(666);
Run Code Online (Sandbox Code Playgroud)

您可以在其中设置所需的退出代码而不是666