Prettier Apex

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

A plugin for Prettier that formats Salesforce Apex code. Current stable version is 2.2.6, maintained by the Salesforce developer community. It supports all Apex syntax including triggers, classes, and anonymous blocks. Release cadence is irregular but active. Key differentiators: it is the only actively maintained Prettier plugin for Apex, integrates seamlessly with Prettier's CLI and APIs, and supports modern Apex features like lambda expressions and enhanced loops.

error Cannot find module 'prettier-plugin-apex'
cause Plugin not installed or missing dependencies.
fix
Run 'npm install prettier-plugin-apex --save-dev' and ensure Prettier v3 is installed.
error Error: Cannot find module 'prettier-plugin-apex' from ...
cause Plugin is not resolved correctly in Prettier configuration.
fix
Specify full path to plugin in plugins array: plugins: ['./node_modules/prettier-plugin-apex/dist/index.mjs']
error Error: Unknown parser: 'apex'
cause Plugin not properly loaded or Prettier version incompatible.
fix
Ensure Prettier v3 is used and plugin is in plugins array.
error TypeError: prettier.format is not a function
cause Using require() instead of import with ESM-only Prettier v3.
fix
Use dynamic import: const prettier = await import('prettier');
breaking Plugin requires Prettier v3 or higher. Prettier v2 is not supported.
fix Update Prettier to ^3.0.0.
deprecated Option 'apexUseStandardClassSemicolons' is deprecated in v2.0.0.
fix Use 'apexInsertFinalNewline' instead.
gotcha Plugin does not format Apex test methods differently in class bodies.
fix No official support; contributions welcome.
gotcha Plugin is sensitive to Prettier's 'semicolons' option; setting 'semicolons: false' may produce invalid Apex.
fix Keep 'semicolons: true' for Apex code.
npm install prettier-plugin-apex
yarn add prettier-plugin-apex
pnpm add prettier-plugin-apex

Formats a simple Apex class using Prettier with the Apex plugin.

import prettier from 'prettier';
import * as prettierPluginApex from 'prettier-plugin-apex';

const code = `public class MyClass { public static void test() { System.debug('hello'); } }`;

prettier.format(code, {
  parser: 'apex',
  plugins: [prettierPluginApex],
  apexInsertFinalNewline: true,
}).then(result => console.log(result));
// Output:
// public class MyClass {
//   public static void test() {
//     System.debug('hello');
//   }
// }