Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop build programatically in Next.js

Is there a way to stop npm run build programatically in Next.js as soon as a certain Error occurs in getStaticProps? Just throwing an Error does not stop the build process.

like image 486
FelHa Avatar asked Mar 14 '26 16:03

FelHa


1 Answers

I've been trying to achieve the same thing but apparently there is no official way. What I've tried is the following code inside next.config.js:

module.exports = async (phase, { defaultConfig }) => {
  try {
    console.log("fetching app info", phase)
    const appInfo = await fetchAppInfo()
    fs.writeJson("./src/appInfo.json", appInfo)
  } catch (e) {
    console.log(e)
    throw e
  }

  return yourConfig
}

If you don't return the expected config for next js, the build procedure will be cancled

like image 159
Ardalan Avatar answered Mar 16 '26 06:03

Ardalan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!