joshua1991
joshua1991

Reputation: 129

How to show error console log for stylelint Node API?

When using the stylelint on the CLI like stylelint "**/_main.scss" and there's an error in the file, it errors out and logs something like enter image description here

However when using the Node API for it, the log is just an output with some key values with the error as a string value. How do I get the output to error out just like when using the CLI? Thank you.

Upvotes: 0

Views: 654

Answers (1)

jeddy3
jeddy3

Reputation: 3959

The default formatter for the stylelint Node API is "json", whereas the stylelint CLI uses the "string" formatter.

You can use the formatter property to use the "string" formatter when using the Node API, like so:

var stylelint = require("stylelint");

stylelint
  .lint({
    code: "a { unknown: 0 }",
    config: { rules: { "property-no-unknown": true } },
    formatter: "string"
  })
  .then(function({ output, errored }) {
    console.log(output);
    if (errored) process.exit(2);
  })
  .catch(function(err) {
    console.error(err.stack);
  });

The Developer guide documentation details the structure of the returned promise. You can use output to display the results from the formatter and errored to set the exit code.

Upvotes: 1

Related Questions