If you have been programming for a while, you would be familiar with the hassles of writing clean code and maintaining consistency across a project on some specific code style guidelines. Even if you spend a few days making everything consistent, some other developer might check-in their changes or disable some flag to quickly get their code in.
Wasting time formatting your code is a monotonous task and Prettier has been designed to solve this problem of wasting time on it. So it helps you overcome formatting fatigue with a very easy setup.
Prettier is an opinionated code formatter and it automates the process of formatting the entire code base. So, after setting up prettier, you no longer need to argue with coworkers about code formatting rules, semicolons, line breaks, etc. It takes in all your code, removes all formatting, and re-formats the code according to its style guidelines.
Under the hood, all the javascript code is converted into Abstract Syntax Tree and then formatted back. So it ensures that there would not be any breaking changes to the code that was written. For you, everything gets formatted magically and you do not have to worry about it.
For example:
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
JavaScriptgets converted to
foo(
reallyLongArg(),
omgSoManyParameters(),
IShouldRefactorThis(),
isThereSeriouslyAnotherOne()
);
JavaScriptIf you want to take it for a spin, use the playground to play with it. To sum it up:
This is one of the most commonly asked questions and the brief answer is that ESLint is a code quality tool (no unused variables, no global variables, prefer promises, etc.). On the other hand, Prettier is only concerned about formatting the file (maximum length, mixed tabs, and spaces, quote style, etc.). You can integrate prettier with ESLint to have a win-win combo.
Prettier version 1.9 onwards reads from editorconfig if it is present. The properties that are respected are: indent_style, indent_size/tab_width and max_line_length.
You can setup Prettier at one of the following levels:
To install it in your IDE/Editor, go to the integrations page on the website and download the corresponding plugin. It should be a pretty simple process. For Visual Studio Code, all you need is to download it from the marketplace. After that, I put in a preference in my user settings ("editor.formatOnSave": true
). And it just worked! (I also have the "prettier.singleQuote": true
setting turned on, but that is a personal preference.) Other editor level configuration can be set here too.
You need to install the prettier CLI first. We will add it as a dev dependency. (yarn add prettier --dev --exact
or npm install --save-dev --save-exact prettier
). Prettier recommends installing the exact version in your project since they tend to introduce stylistic changes in patch releases. After installation succeeds, to run against a specific file, all you need to do now is run the command yarn prettier --write index.js
. This will pretty print the index.js file in its place itself.
You can configure other options that you want prettier to work with, but usually, a configuration file is used for it. These can be added to a .prettierrc file in the root folder of your project. The available options can be found on the website.
You can then add this command to your NPM scripts passing in your src folder as a glob to it instead of a single file. The script for formatting would look like: prettier --write './src/**/*.{ts,js,css,json}'
You can either specify the options using command line arguments here if you are not using multiple of those, or use the .prettierrc file.
And then you can invoke this as a part of your build step. And this will make all the changes in place.
You can go one more step further and integrate prettier as a pre-commit hook in the project. You will need to install husky for this along with either lint-staged or pretty-quick. (Use lint-staged if you are using other tools on top of prettier like ESLint.) After installing these packages as dev dependencies from NPM, you can add the following configuration to your package.json file:
For pretty-quick:
"scripts": {
"precommit": "pretty-quick --staged"
}
JavaScriptFor lint-staged:
"scripts": {
"precommit": "lint-staged",
"lint-staged": {
"*.{ts,js,css,json}": [
"prettier --write",
"git add"
]
}
}
JavaScriptDo note that when you have prettier running as a pre-commit hook, it breaks staging hunks. A workaround (suggested on Reddit by gocarsno) would be to stashing all unstaged changes before committing (git stash -k
)
You can go even further by adding another check in the build step of your project. Facebook does this step and I saw it in one of the employees mention it in a conference talk. You can run prettier on the CI/CD server on the files and if the files output by prettier is different than what is checked in, the server fails the build because of it. It all depends on how aggressively you want to use it.
So what are you waiting for? Go ahead, set up prettier now! And say goodbye to formatting things manually. Let the machine do it for you.
Also, do share this post and let more people discover about prettier. 😉
I am terrible at optimizing my keyboard layout for anything. But off lately, my little…
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…
View Comments
What I am waiting for is an example of use on the command line, where the --write option is not used, and I can specify a destination path for result files.
I think prettier is for someone who doesn't want to bother with any config just to make the code "clean" (person like me). In IntelliJ family I have familiarized with CTRL+ALT+L which will reformat our code. Prettier is my savior in editor like VSCode. Before I knew Prettier sometimes I must to indentation manually with TAB. Now, I don't have to bother.
Prettier is much more than the formatting done by IDE's. But yes, it's major benefit is that it just works!
You might want to consider changing the installation command to have `--exact`. From the docs at https://prettier.io/docs/en/install.html
> We recommend pinning an exact version of prettier in your package.json as we introduce stylistic changes in patch releases.
Added it. Thanks for pointing that out!
I'm confused...ESLint can also be concerned about formatting a file:
* https://eslint.org/docs/rules/max-len
* https://eslint.org/docs/rules/no-mixed-spaces-and-tabs
* https://eslint.org/docs/rules/quotes
It seems the only argument for Prettier is forced format on save and no config?
ESLint will only warn you. Prettier formats it automatically. Also this issue might help: https://github.com/prettier/prettier-eslint/issues/101