node-sass
node-sass copied to clipboard
CLI: Error output style
This might be a dumb question/request, but it seems that there's no information about that:
If you are using the CLI to compile SCSS to CSS and there's an error in your code, the error is outputted in json format:
{
"formatted": "Error: Undefined variable: \"$variable\".\n on line X of X\n>> \n ----^\n",
"message": "Undefined variable: \"$variable\".",
"column": 1,
"line": 1,
"file": "FILE_NAME",
"status": 1
}
For some error messages this is really hard to read. Is there any way to output error message as "plain text" or, if not, any plans to implement this feature?
Thanks for the suggestion @demrks. This is something I have thought about.
@am11 any idea whey we spit out json to standout? I have a feeling it changed in 3.3 ish.
I'm would like to change this output be more useful. This will likely have to come on a major version since someone in inevitably relying on parsing this json.
@xzyfer In order to be backwards compatible, you could also add an additional parameter (defaults to json format) to the CLI, so people can choose what output they prefer. Something like
node-sass --error-output-style json
@xzyfer, this was a v2.0 change (https://github.com/sass/node-sass/commit/9f7a78d870cede1a55e6ab342ab133815e62bbf9), when libsass implemented JSON serialization and we decided to get error as JSON string from libsass, serialize it as JS Error
object and output the object in CLI scenario (errObj.toString()
). At that point, formatted
option was not added to the error object, so that was the simple way to convey the error with line/file info to the user.
Now that we have formatted
option from libsass plus we do not follow that convention everywhere, i.e. some errors are not serialized as JSON:
- "No input file was found"
- when
.sass
or.scss
file path is provided, but file does not exist on FS
- when
- "An output directory must be specified when compiling a directory"
- when some path is provided, which does not have
.sass
or.scss
ext. and-o
option is missing
- when some path is provided, which does not have
- "Provide a Sass file to render"
- when no input path is provided
it probably makes sense to introduce a breaking change in render.js
: emitter.emit('error', chalk.red(JSON.stringify(error, null, 2)));
to emitter.emit('error', chalk.red(error.formatted));
. Also, for those three string errors which are directly sent to emitter without ever being real Error
object, those should use the chalk.red
for consistency.
Thanks for the feedback @am11. Always appreciated.
Will this change be implemented, @am11? It would really help.
@robrecord this will land in node-sass@v4 because it's a breaking change. Node Sass 4 will likely land when LibSass reaches 3.4.0
In the mean time if you want better formatted errors then pass stderr
to prettyjson
. Something like this:
node-sass source/scss/main.scss public/css/main.css 2>&1 | prettyjson
@darren131 I tried this approach, and it does work as long as there is an actual error, since prettyjson gets passed valid json. But when there is no error 2>&1
pipes a string ("Rendering Complete, saving .css file...") causing an error in prettyjson.
Being a command line rookie, I couldn't quite figure out how to fix this, any suggestions?
So are we going to change the error style?