In English /

Lint-staged with Prettier proper configuring

cover image

One of the most common use cases for lint-staged is formatting files using Prettier. Documentation of lint-staged is full of references to this use case, suggesting you to achieve it this way:

.lintstagedrc
{
  "*": "prettier --write"
}

However, if you just setup it this way, at some point you may encounter an error like this when trying to commit your changes:

✖ prettier --write: [error] No parser could be inferred for file "..."

It may happen if your changes are including some files with a format not supported by Prettier, like for example a binary file, image, etc. The reason for this is that behind the scene lint-staged invokes Prettier with an exact file path, and in this case Prettier assumes that this file must be formatted, despite the format. So, the error from Prettier seems to be logical. This behavior is different compared to when we run Prettier by ourselves, passing it a glob pattern instead of an exact file path, like prettier --write .. It will work as expected and will ignore unsupported file formats.

Luckily in case of lint-staged the solution is pretty easy. It is mentioned in the lint-staged documentation, but you must be very attentive in order to find it :). The solution is simply adding an --ignore-unknown flag when running Prettier:

.lintstagedrc
{
  "*": "prettier --write --ignore-unknown"
}

This flag tells Prettier to ignore unknown files even if it's invoked with an exact file path, which is just what we need.

send feedback

Сайт построен с помощью Gatsby 🚀 и опубликован на Netlify .
Исходный код доступен на GitHub .

Сайт обновлен:

© Сергей Ермакович
Данный сайт не использует инструменты для отслеживания пользовательской активности 🙅‍♂️

WSSE - быстрые и надежные сайты