Prettier Plugin for Solidity

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

A Prettier plugin for automatically formatting Solidity smart contract code. Current version is 2.3.1, with major version 2 (2024) switching from ANTLR to Nomic Foundation's Slang parser, improving correctness and comment handling. Requires Node >=20 and Prettier >=3.0.0. Formally maintained, with frequent patch releases. Key differentiator: it is the de facto standard formatter for Solidity, tightly integrated with Prettier's ecosystem.

error Error: Couldn't resolve plugin "prettier-plugin-solidity"
cause Plugin not installed or not in node_modules.
fix
Run 'npm install --save-dev prettier prettier-plugin-solidity'.
error Error: Cannot find module 'prettier-plugin-solidity'
cause Incorrect plugin name or path in .prettierrc.
fix
Set plugins array correctly: ["prettier-plugin-solidity"].
error TypeError: Invalid Version: undefined
cause Missing or malformed compiler option in .prettierrc.
fix
Add a valid compiler version string, e.g., "compiler": "0.8.0".
breaking v2.0.0 switched default parser from 'solidity-parse' (ANTLR) to 'slang'. Old configs with 'solidity-parse' will cause errors.
fix Update .prettierrc: set parser to 'slang' (or remove explicit parser).
deprecated The 'solidity-parse' parser is deprecated in v2 and will be removed in a future major release.
fix Use 'slang' parser instead.
gotcha Node >=20 is required. Older Node versions will fail at install time.
fix Upgrade Node to 20 or later.
gotcha Peer dependency on Prettier >=3.0.0. Prettier v2 is not supported.
fix Upgrade Prettier to 3.x.
npm install prettier-plugin-solidity
yarn add prettier-plugin-solidity
pnpm add prettier-plugin-solidity

Installs the plugin, configures Prettier for Solidity using Slang parser, and runs formatting.

// Install: npm install --save-dev prettier prettier-plugin-solidity
// .prettierrc (JSON)
{
  "plugins": ["prettier-plugin-solidity"],
  "parser": "slang",
  "tabWidth": 4,
  "overrides": [{
    "files": "*.sol",
    "options": {
      "compiler": "0.8.0"
    }
  }]
}
// Run: npx prettier --write 'contracts/**/*.sol'