1

I have an npm script where one of the commands, in this case, the test script, can fail.

"test": "npm run init && npm run test && npm run end"

If the test script fails the end script is never executed.
Is there a way to force the execution of the end script even if test fails? Thanks!

Peter
  • 2,004
  • 2
  • 24
  • 57
  • 1
    Does this answer your question? [How to continue running scripts defined in package.json in case one fails?](https://stackoverflow.com/questions/62343768/how-to-continue-running-scripts-defined-in-package-json-in-case-one-fails) – RobC Aug 26 '21 at 11:47

1 Answers1

5

Use ; control operator:

npm run init ; npm run test ; npm run end

; means execute the preceding statement and, after it completes, proceed to the next statement.

alexmac
  • 19,087
  • 7
  • 58
  • 69
  • Does this not also not change the exit code? Now the exit code is only reliant on the exit code from the last command as opposed from any of them. In other words, if the first command fails, but the last succeeds, it will possibly misleadingly report a success. – Chris Stryczynski May 02 '23 at 13:04