skip to Main Content

I have an NPM script that runs multiple tasks like this:

"multiple": "task1 && task2 && task3"

However if tasks2 fails, the script will terminate and task3 will not be run.

Is there a way to "Catch" / "Ignore" the error if it does occur, so that task3 runs either way?

Something like:


"multiple": "task1 && task2 > ignore && task3"

2

Answers


  1. Use ; instead of &&:

    "multiple": "task1 && task2 ; task3"
    

    && is logical "and" with short-circuit. It stops the chain immediatly when the result is clear (false) and doesn’t evaluate the remaining operations.

    Login or Signup to reply.
  2. to continue running all tasks even if one of them fails by adding the --ignore-scripts flag.

    Here’s how I would modify the package.json file:

    "scripts": {
      "my-script": "task1 && task2 && task3 || true"
    }
    

    In the example above, the && operator is used to run each task sequentially. The || true statement is added at the end to ensure that the script continues to run even if one of the tasks fails.

    OR you could try the --force flag, which will force npm to continue running the script even if errors are thrown.

    "scripts": {
      "my-script": "npm run task1 && npm run task2 && npm run task3 --force"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search