Prettier Terraform Formatter Plugin
raw JSON → 1.2.1 verified Sat Apr 25 auth: no javascript
A Prettier plugin that formats Terraform files by shelling out to the local `terraform fmt` command. Version 1.2.1 is the current stable release, with a maintenance cadence. Unlike other Terraform formatters (like `terraform fmt` directly), this integrates with Prettier workflows and respects Prettier's file matching and option system. The Terraform CLI must be installed separately. Supports `terraformStrictError` option to control error behavior when terraform is not found.
Common errors
error Error: Cannot find module 'prettier-plugin-terraform-formatter' ↓
cause Plugin not installed or missing from node_modules.
fix
Run: npm install prettier-plugin-terraform-formatter as a dev dependency.
error [error] Invalid .prettierrc: plugins must be an array ↓
cause Incorrect Prettier configuration format.
fix
Ensure plugins field is an array: 'plugins': ['prettier-plugin-terraform-formatter']
error terraform: command not found ↓
cause Terraform CLI is not installed or not in PATH.
fix
Install Terraform (e.g., via tfenv or hashicorp tap) and ensure it's executable.
Warnings
gotcha Plugin silently ignores tf files if terraform CLI is not installed or fails (non-zero exit code other than 2). ↓
fix Set 'terraformStrictError' to true to treat all failures as errors, or ensure terraform is installed and in PATH.
gotcha The plugin shells out to terraform fmt for each file, which can be slow for many files. ↓
fix Consider running terraform fmt directly for bulk formatting, or use Prettier's caching.
gotcha Different terraform versions may produce different formatting results; plugin does not pin terraform version. ↓
fix Ensure consistent terraform version across team (e.g., via tfenv or Docker).
Install
npm install prettier-plugin-terraform-formatter yarn add prettier-plugin-terraform-formatter pnpm add prettier-plugin-terraform-formatter Imports
- plugin wrong
const plugin = require('prettier-plugin-terraform-formatter')correct// No explicit import needed, Prettier autoloads plugin - options wrong
import { terraformStrictError } from 'prettier-plugin-terraform-formatter'correct// In .prettierrc: { "plugins": ["prettier-plugin-terraform-formatter"], "terraformStrictError": true }
Quickstart
// 1. Install: npm i -D prettier prettier-plugin-terraform-formatter
// 2. Ensure terraform CLI is installed: which terraform
// 3. Create .prettierrc:
{
"plugins": ["prettier-plugin-terraform-formatter"],
"overrides": [
{
"files": "*.tf",
"options": {
"parser": "terraform"
}
}
]
}
// 4. Format: npx prettier --write main.tf