Npm error status in bat file

We are currently compiling a collection of our user interface projects (total 3) for Grunt. To facilitate the transition, I would like to provide a bat file that will run npm installfor each project, however I would like to know that something went wrong when issuing this command. This is just the sugar coating I need, I know that npm causes errors in the echo, but I would like to receive simpler messages for members of my team who are not familiar with npmand node.

Is there any way to check if npm is running in error and subsequently stop the bat file? For example, if node is not installed, I just check %ERRORLEVEL%for 1. If so, I repeat some instructions and complete the execution. The problem that I encountered is that it is %ERRORLEVEL%not set to 1 when an error occurs during npm install.

Any help appreciated!

+4
source share
2 answers

Some commands do not update ERRORLEVEL. I'm not sure how about installing node.js, but I ran into the same problem with the command NSLOOKUP. NPMseems to work in its own instance, which may explain why it is not updating ERRORLEVEL.

, , . , , "" . :

    cmd /c "npm install > install.log 2>&1"
    for /f %%a in ('type install.log ^| find /c /i "err"') do set err=%%a
    if "%err%" GTR "0" echo ERROR was encountered during installation

:

  • cmd /c "npm install > install.log 2>&1"

    install.log. 2>&1 . , . cmd /c NPM. .

  • for /f %%a in ('type install.log ^| find /c /i "err"') do set err=%%a

    install.log err , "err".

  • if "%err%" GTR "0" echo ERROR was encountered during installation

    err 0, echo .

node.js, , NPM ( ). , node.js .

+2

.

, npm ( test.bat), :

@ECHO OFF
call npm install jquery 2<&1 || exit /b 1
ECHO Continuing...

, test.bat, :

jquery@2.1.3 node_modules\jquery
Continuing...

, "jquery" , (.. npm, , , % ERRORLEVEL%), :

npm ERR! Windows_NT 6.2.9200
npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "asdasdas"
npm ERR! node v0.10.33
npm ERR! npm  v2.3.0
npm ERR! code ETARGET

npm ERR! notarget No compatible version found: undefined@'*'
npm ERR! notarget No valid targets found.
npm ERR! notarget Perhaps not compatible with your version of node?
npm ERR! notarget This is most likely not a problem with npm itself.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Projects\XXXX\npm-debug.log

. "ECHO Continuing" .

"2<&1", , STDERR STDOUT: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

"... handle 2 ( , STDERR), ipconfig 1 ( , STDOUT)..."

fooobar.com/questions/298242/...

+2

All Articles