How to make a Gatsby build terminate on error

Oscar de la Hera Gomez
A flower that represents Gatsby with the text 'Terminate Build' beneath it.

To cancel, end or terminate a Gatsby build call the GatsbyReporter panic function.

A screenshot of Terminal showing that the Gatsby build continues even if create pages succeeds.

An example of create pages having failed but the build continued anyway.

During our time using Gatsby we noticed that builds keep running even if they fail - for example, if a GraphQL query takes longer than it should on the backend.

This implies that, under this mode of operation, you could release a broken or incomplete site.

So how do you make a build exit or cancel, if something goes wrong during the build process?

Use GatsbyReporter's panic function

A screenshot of Terminal showing the build failing with an error and terminating the build.

GatsbyJS's node functionality includes a reporter known as GatsbyReporter which offers various functions including:

  • panic: which cancels a build and prints an error
  • panicOnBuild: which prints an error but does not cancel a build.

To see it in use, check out the gist below:

Any Questions ?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.

delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details