Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch npm ERR! when running npm test from shell script

Tags:

shell

node.js

npm

I have a shell script that recursively searches for package.json from a base directory and runs npm test like so.

find "$parent_dir" -name package.json -maxdepth 2 -execdir npm test \;

I want to intercept tests failure in the form of:

npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

So I can have the shell script exit with an error status.

Any help is appreciated.

like image 523
i_trope Avatar asked Jun 29 '15 19:06

i_trope


People also ask

What is the use of npm test command?

The npm test command is used to test a package. This command runs the test script that is contained in a package, if you provided one.

How do I resolve npm start error?

To solve the npm ERR! Missing script: "start" error, make sure to add a start command to the scripts object in your package. json file and open your shell or IDE in the root directory of your project before running the npm start command.


2 Answers

Another 3 year late and was looking for solution in this too.

Finally found it and if these help others in future. I used shell script $? command, which returns the exit status of the last executed command. I.e. if a shell script fails it returns 1 while a 0 if it's success. NPM implements this return hence.

npm run test
if [ $? -eq 0 ]
then
  echo "SUCCESS"
else
  echo "FAIL"
fi
like image 97
Han Avatar answered Sep 23 '22 03:09

Han


If I understood you correctly you want to execute something or stop execution of script if npm ERR! has been thrown? This might help:

 if ./somecommand | grep -q 'npm ERR!'; then
      #what you want to do
      # exit executing the script
      return
    fi

Sorry what I am two years late, just joined the SO community.

like image 27
Delmys Asteroraena Avatar answered Sep 23 '22 03:09

Delmys Asteroraena