prettier-plugin-erb

raw JSON →
0.4.0 verified Sat Apr 25 auth: no javascript

A Prettier plugin for formatting .html.erb files, currently at version 0.4.0. It integrates with Prettier (>=2.0.0 <2.3) and depends on @prettier/plugin-ruby for formatting multiline Ruby expressions. Key differentiators: allows ERB formatting within Prettier's ecosystem, supports single-line expressions, and includes basic comment handling. The plugin is relatively new with limited features (no block expression formatting yet) and may have bugs; users are encouraged to ignore problematic files via .prettierignore.

error SyntaxError: Unexpected token '>'
cause Prettier version >=2.3 is incompatible with this plugin.
fix
Downgrade Prettier to version 2.2.x or lower.
error Error: Cannot find module '@prettier/plugin-ruby'
cause Missing peer dependency @prettier/plugin-ruby.
fix
Install the missing peer: yarn add -D @prettier/plugin-ruby
error Error: [object Object]
cause Bug in older versions (<0.4.0) when formatting certain ERB expressions.
fix
Upgrade to version 0.4.0 or later.
error Cannot read property 'type' of undefined
cause Block expressions (e.g., <% if %>) not supported; the plugin cannot parse them.
fix
Remove or rewrite block expressions as single-line ERB, or add file to .prettierignore.
breaking The plugin requires Prettier <2.3 due to compatibility issues with Prettier options.
fix Use Prettier version 2.2.x or lower. Check peerDependencies: prettier >=2.0.0 <2.3.
gotcha Block expressions (e.g., <% if … %> … <% end %>) are not formatted; only single-line expressions work.
fix Upgrade to v0.2.0+ for single-line expressions; block expressions still not supported in any version.
gotcha The plugin may return `[object Object]` as output in edge cases (fixed in v0.4.0).
fix Upgrade to v0.4.0 or later.
deprecated The `print` function replaces `embed` for error logging (v0.2.0+).
fix Upgrade to v0.2.0+ if you rely on error formatting.
gotcha The plugin may format files incorrectly; add problematic files to .prettierignore.
fix Use .prettierignore to exclude files that cause errors or unwanted formatting.
npm install prettier-plugin-erb
yarn add prettier-plugin-erb
pnpm add prettier-plugin-erb

Shows installation, configuration in .prettierrc, and CLI usage for formatting .html.erb files.

// Install dependencies
// yarn add -D prettier@2.2.0 @prettier/plugin-ruby@1.0.0 prettier-plugin-erb@0.4.0

// .prettierrc
{
  "plugins": [
    "@prettier/plugin-ruby",
    "prettier-plugin-erb"
  ],
  "tabWidth": 2
}

// Run
// npx prettier --write app/views/**/*.html.erb