Prettier is an opinionated code formatter.
- Install latest Prettier.
npm install --save-dev --save-exact prettier
- Add
.prettierignore
. Prettier will follow rules in.gitignore
# Ignore artifacts:
build
coverage
- To check all files:
npx prettier . --check
- To format all files:
npx prettier . --write
Prettier will search for a configuration file in the following order:
prettier
key in your package.json file..prettierrc
file written in JSON or YAML..prettierrc.json
,.prettierrc.yml
,.prettierrc.yaml
, or.prettierrc.json5
file..prettierrc.js
, orprettier.config.js
file that exports an object using export default or module.exports (depends on the type value in yourpackage.json
)..prettierrc.mjs
, orprettier.config.mjs
file that exports an object using export default..prettierrc.cjs
, orprettier.config.cj
s file that exports an object using module.exports..prettierrc.toml
file.
Many values will inherit from .editorconfig
, if it exists.
- Use globs in
.prettierignore
# Ignore artifacts:
build
coverage
# Ignore all HTML files:
**/*.html
- Using the CLI
prettier . "!**/*.{js,jsx,vue}" --write
- Use comments in JavaScript
// prettier-ignore
matrix(
1, 0, 0,
0, 1, 0,
0, 0, 1
)
- Use comments in JSX
<div>
{/* prettier-ignore */}
<span ugly format='' />
</div>
- Use comments in HTML
<!-- prettier-ignore -->
<div class="x" >hello world</div >
- Use comments in CSS
/* prettier-ignore */
.my ugly rule
{
}
- Use comments in Markdown
<!-- prettier-ignore -->
Do not format this
- Use comments in YAML
# prettier-ignore
key : value
hello: world
esbenp.prettier-vscode
{
"prettier.prettierPath": "./node_modules/prettier",
"prettier.enable": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}