How to make a Gatsby build terminate on error

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

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 09/14/2022 at 11:46
Last Updated on 09/14/2022 at 12:06
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:

Learn more about Gatsby Reporter

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.

SubscribeInquireView All PostsView All ServicesView All Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe