Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query npm error state in bat file

We're currently moving the build of our UI projects (3 in total) to Grunt. To ease the transition I'd like to provide a bat file that will run npm install for each project, however I'd like to know if something went wrong when issuing this command. It's merely sugar coating that I'm after, I know npm echoes errors out, but I'd like some easier messages for the members on my team that are not familiar with npm and node.

Is there a way to check whether npm has run into an error and to subsequently halt the bat file? For example, if node is not installed, I simply check for %ERRORLEVEL% to be 1. If this is the case, I echo out some instructions and exit the execution. The problem I'm having is that %ERRORLEVEL% is not set to 1 when an error occurs during a npm install.

Any help appreciated!

like image 514
thomaux Avatar asked Jan 02 '14 12:01

thomaux


People also ask

Why is npm install not working on CMD?

The Npm command not found error can appear when you install or upgrade npm. On Windows, the cause of this error could be that a PATH or system variable is not correctly set. The error can also occur if you do not have npm or Node. js installed, have an outdated version, or have permission issues.

What is Errorlevel in batch file?

Batch file error level: %ERRORLEVEL% is an environment variable that contains the last error level or return code in the batch file – that is, the last error code of the last command executed. Error levels may be checked by using the %ERRORLEVEL% variable as follows: IF %ERRORLEVEL% NEQ 0 ( DO_Something )

How do I make a batch file terminate upon encountering an error?

It is common to use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file. EXIT /B at the end of the batch file will stop execution of a batch file.


2 Answers

Try this way.

In my batch file that runs npm (let's call it test.bat) I have this:

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

Now, when I run test.bat I see this:

[email protected] node_modules\jquery
Continuing...

However, if I change "jquery" to the name of a package that doesn't exist (i.e. simulate an npm failure, that as you say doesn't affect %ERRORLEVEL%) I see this:

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

Note, the "ECHO Continuing" line is not executed.

The "2<&1" part seems to take STDERR output and redirect it to STDOUT: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

"... handle 2 (that is, STDERR), from the ipconfig command to handle 1 (that is, STDOUT)..."

I took this solution from https://stackoverflow.com/a/10359327/68432

like image 66
Paul Suart Avatar answered Oct 20 '22 22:10

Paul Suart


Some commands do not update ERRORLEVEL. I am not really sure how about node.js installation, but I have encountered the same issue with the NSLOOKUP command. NPM seems to be running in its own instance, which might be explain why it's not updating ERRORLEVEL.

I can suggest you write the output of the command into some log file using simple redirection, then search the file for text indicating an unsuccessful install. Let's say you're looking for the number of times the word "error" occurs. The batch file can look like this:

    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

To explain it bit more step by step:

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

    Perform the installation and redirect the output to install.log. Note the 2>&1 at the end. This is to save both error output and standard to a single file. The reason for cmd /c is to close the instance npm will create. Without that I couldn't make this work.

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

    Go through install.log and set the variable err to the number of lines containing 'err'.

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

    If err is greater than 0 then echo a custom message.

Another method could be to perform a basic test of the parts of node.js you need, like executing some npm commands and checking their output (similar to the above). However, I am not an expert in node.js and can't guide you further in this point.

like image 26
R1cky Avatar answered Oct 20 '22 22:10

R1cky