10

Using jshint-loader with Webpack, how do I make the webpack command fail when JSHint emits warnings?

The context being that I wish to fail the CI build if linting detects issues.

Currently, I've simply configured Webpack to run jshint-loader on preload of JS files:

// webpack.config.js
module.exports = {
  module: {
    preLoaders: [
      {
        test: /\.js/,
        exclude: /node_modules/,
        loader: 'jshint-loader',
      },
    ],
  },
};

1 Answer 1

5

First, jshint-loader must be configured to fail in case issues are found (failOnHint: true), optionally one can also choose to emit warnings as Webpack errors (emitErrors: true).

// webpack.config.js
module.exports = {
  module: {
    preLoaders: [
      {
        test: /\.js/,
        exclude: /node_modules/,
        loader: 'jshint-loader',
      },
    ],
  },
  jshint: {
    emitErrors: true,
    failOnHint: true,
  },
};

Second, Webpack must be told to fail hard, by supplying the --bail option: webpack --bail.

Update:

webpack --bail still doesn't emit a non-zero exit code, argh.

4
  • Hm, how come webpack complains that '"jshint" is not allowed' when I add a jshint option to the config? Sep 29, 2016 at 11:54
  • Ah, because of webpack validator, I needed to add a custom rule to the validator schema. github.com/js-dxtools/webpack-validator#schema Sep 29, 2016 at 12:05
  • I'm having the same problem, but this solution doesn't help. I am still getting warnings and "module build failed", but it still results in succesful code 0. Oct 21, 2016 at 13:33
  • @SergejsRižovs Back then I eventually realized that webpack --bail returns zero even in the case of failure (see the Update bit at the end of the answer). I don't know how it is now, as I haven't tested it for some time.
    – aknuds1
    Oct 21, 2016 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.