How to catch the Nuxt build error

2019-10-03

The compile error must be catched when deploying the Nuxt or Vuejs project to the production server from a command line.

For example, the following situations.

  1. nuxt building on a docker container.
  2. `docker commit` to create an image from docker container.
  3. deploying image to Kubernetes, for example.

Building from Nodejs

It is easy to catch an build error because of promise callback with nodejs.

const {Nuxt, Builder} = require("nuxt");
let config = require("./nuxt.config.js");
config.dev = false; // production build
config.mode = 'universal';
let nuxt = new Nuxt(config)

new Builder(nuxt).build()
.then(() => {})
.catch(() => {
	// Build Error
});

The Nuxt build API makes it possible to catch an build error.

Building from shell command

In most cases, the projects are deployed by shell command for example in CI system.

nuxt build:

npm run build
or
nuxt build

Bad way evaluating an error is whether the stdout in a build command includes “Error" or not. On the other, it is very simple way evaluating an error by exit code with the following.

#!/bin/bash
npm run build
if [ $? != 0 ] ; then
  echo -e "33[0;31mBuild Error!33[0;39m"
  exit 1
fi

Most compile command return exit code. so the code is used for conditional execution with other web framework.