Running Prettier in CI/CD
What you'll learn: How to enforce code formatting standards automatically by running Prettier in your continuous integration pipeline.
Why Check Formatting in CI?
You've configured Prettier locally and maybe even set up format-on-save in your editor. But what happens when a teammate forgets to format before pushing code? Or when someone bypasses the pre-commit hook?
That's where CI/CD integration comes in. By adding Prettier to your continuous integration pipeline, you create an automated gatekeeper that prevents unformatted code from being merged.
Think of it like a security checkpoint at an airport—no matter how careful people are at home, everyone passes through the same verification before boarding.
The --check Flag
The key is Prettier's --check flag. Unlike the default behavior (which reformats files), --check only verifies that files are already formatted correctly. If any file would change, Prettier exits with an error code and lists the offending files.
npx prettier --check .
This command checks all files in your project (respecting .prettierignore). If formatting is perfect, it succeeds silently. If not, it fails and your CI build breaks.
Adding to Your CI Pipeline
In your CI configuration (GitHub Actions, GitLab CI, CircleCI, etc.), add a step that runs this check:
# Example for GitHub Actions
- name: Check code formatting
run: npx prettier --check .
You can also add this as an npm script in package.json:
"scripts": {
"format:check": "prettier --check ."
}
Then run npm run format:check in CI.
The Safety Net Effect
This approach doesn't fix formatting issues—it just prevents unformatted code from being merged. Developers must run prettier --write . locally to pass the check.
Key Takeaway: Use prettier --check in CI pipelines to enforce formatting standards automatically, catching any code that wasn't properly formatted before merging.