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!
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.
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:
jquery@2.1.3 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