prettier-plugin-powershell
raw JSON → 2.1.0 verified Sat Apr 25 auth: no javascript
Prettier plugin (v2.1.0, monthly releases) for formatting PowerShell source files (.ps1, .psm1, .psd1) with idiomatic defaults. Supports fine-grained configuration like indentation style/width, trailing commas, brace style, alias rewriting, and keyword casing. Requires Node.js >=20.11.0, Prettier >=3, and has an optional peer dependency on TypeScript ^6.0.3 for types. Extensively tested with ≥95% coverage, ready for CI/CD pipelines. Differentiator: first-class Prettier integration with options beyond basic PowerShell formatters.
Common errors
error Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'prettier-plugin-powershell' imported from... ↓
cause Missing plugin in package.json dependencies or incorrect import path.
fix
Run
npm install --save-dev prettier-plugin-powershell and verify the package is in node_modules. error TypeError: Cannot read properties of undefined (reading 'powershell') ↓
cause Parser not set to 'powershell' when using plugin.
fix
Add 'parser: "powershell"' to Prettier options or config file.
error SyntaxError: Unexpected token 'export' ↓
cause Using CommonJS require() with ESM-only package.
fix
Switch to ESM import (import ... from ...) or use dynamic import().
Warnings
breaking Plugin is ESM-only since v2.0.0. CommonJS require() will throw. ↓
fix Use ESM import or dynamic import().
breaking Node.js >=20.11.0 required since v2.0.0. ↓
fix Upgrade Node.js to 20.11.0 or higher.
breaking Prettier v3 or higher required since v2.0.0. ↓
fix Upgrade Prettier to v3+.
deprecated Option powershellRewritAliases (note: missing 'e') removed in v2.0.0; use powershellRewriteAliases (correct spelling). ↓
fix Rename option to powershellRewriteAliases.
gotcha Plugin must be listed in Prettier config's `plugins` array AND parser must be set to 'powershell'. ↓
fix Set both 'plugins: ["prettier-plugin-powershell"]' and 'parser: "powershell"' .
gotcha Formatting multi-line strings and here-strings may differ from native PowerShell expectations; always verify output. ↓
fix Use powershellIndentHereStrings option to control indent behavior.
Install
npm install prettier-plugin-powershell yarn add prettier-plugin-powershell pnpm add prettier-plugin-powershell Imports
- default wrong
const plugin = require('prettier-plugin-powershell')correctimport plugin from 'prettier-plugin-powershell' - powershell wrong
import { powershell } from 'prettier'correctimport { powershell } from 'prettier-plugin-powershell' - Plugin wrong
import { Plugin } from 'prettier-plugin-powershell'correctimport type { Plugin } from 'prettier-plugin-powershell'
Quickstart
import prettier from 'prettier';
import plugin from 'prettier-plugin-powershell';
const source = `$greeting = "Hello, World!"
Write-Output $greeting
`;
const formatted = await prettier.format(source, {
filepath: 'script.ps1',
parser: 'powershell',
plugins: [plugin],
tabWidth: 4,
powershellTrailingComma: 'all',
});
console.log(formatted);