To automate Prettier for your project, you can follow these steps:
Install Prettier: First, you need to install Prettier as a development dependency in your project.
npm install --save-dev prettier
Create a Prettier configuration file: Create a .prettierrc
file in the root of your project to define your Prettier configuration. For example:
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5"
}
Add a Prettier script: Add a script to your package.json
to run Prettier.
"scripts": {
"prettier": "prettier --write ."
}
Set up a pre-commit hook: Use husky
and lint-staged
to run Prettier on staged files before each commit.
npx husky-init && npm install
npm install --save-dev lint-staged
Update your package.json
to include lint-staged
configuration:
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": "prettier --write"
}
Run Prettier: You can now run Prettier manually using the script you added:
npm run prettier
This setup will ensure that Prettier is automatically run on your codebase, keeping your code consistently formatted.