Add predictable formatting and a staged-file commit hook to an Angular repository, then run project-wide checks in CI. Hooks are convenient feedback, not an enforcement boundary.
This setup adds formatting to an existing Angular repository managed with npm and Git. It keeps the commit hook quick by formatting staged files, while CI checks the full repository. Run the commands from the directory containing package.json and .git.
1. Install the tools
Pin development-tool versions and commit the lockfile so local and CI runs use the same formatter. The versions below were checked for this revision; use a Node.js release supported by your Angular workspace and these tools.
npm install --save-dev --save-exact prettier@3.10.1 husky@9.1.7 lint-staged@17.6.1
npx husky initHusky init creates a pre-commit file and adds a prepare script. Review the changes if you already have hooks or a prepare script; combine the existing workflow instead of replacing it without checking.
2. Set formatting rules and exclusions
{
"singleQuote": true,
"semi": true,
"printWidth": 100
}node_modules/
dist/
coverage/
.angular/
package-lock.jsonAdd or merge these entries into the scripts object in package.json. They check or format supported source and configuration files; generated files stay excluded.
{
"format": "prettier . --write",
"format:check": "prettier . --check"
}3. Run formatting on staged files
{
"*.{ts,js,mjs,cjs,json,html,css,scss,md,yml,yaml}": "prettier --write"
}This single formatter task avoids overlapping commands editing the same file concurrently. If you add ESLint for TypeScript, use an ordered command array for that glob and keep other file globs separate. Preserve the Angular workspace’s existing ESLint configuration.
npx lint-stagedReplace the generated hook body with the line above. lint-staged supplies staged filenames to Prettier and stages its changes. Review the diff before committing, particularly when a file contains both staged and unstaged edits.
4. Check the whole project in CI
npm ci
npm run format:check
npm run build
# If the workspace has a configured Angular test target:
npm test -- --watch=falseNew Angular CLI projects use Vitest for unit testing. Older repositories may still use Karma or a custom runner. Use the test command configured for that repository; ChromeHeadless is not a universal option for every runner.
A Git hook can be skipped or disabled, and a formatting check does not detect every functional defect. Configure the repository’s CI and merge rules to require the checks that matter: formatting, linting where configured, compilation and meaningful tests.
5. Verify before sharing the configuration
- Run npm run format and inspect the changes.
- Stage a deliberately unformatted supported file, run npx lint-staged and inspect the staged result.
- Run npm run format:check and the project’s build and test commands.
- Check that a fresh clone installs the hooks and that CI does not depend on a developer’s global tools.
Documentation and references
Working with Angular and Prettier? Tell us what you're building and our engineers will tell you how they would approach it.
Talk to our team →