Prettier Kotlin Plugin
raw JSON → 2.1.0 verified Sat Apr 25 auth: no javascript
Prettier plugin for Kotlin language formatting. v2.1.0 (latest) ditched the original JS-based printer in favor of kastree's Kotlin Writer, a significant breaking change from v1.x. The parser (kotato) is alpha-stage, based on kastree, and can parse most Kotlin code but has performance issues. Release cadence is irregular with long gaps. Key differentiator: integrates Kotlin formatting into Prettier's ecosystem, allowing uniform code style across JS/TS/CSS/Kotlin projects. Lacks stable and full Kotlin grammar support; not recommended for production.
Common errors
error Cannot find module 'prettier-plugin-kotlin' ↓
cause Plugin not installed or not in node_modules.
fix
Run 'npm install --dev prettier prettier-plugin-kotlin' in your project root.
error Error: Cannot parse file. Unsupported syntax: ... ↓
cause The kotato parser does not support all Kotlin syntax or the file contains errors.
fix
Simplify the code or fix syntax errors. For advanced features, use ktlint instead.
error Cannot read properties of undefined (reading 'type') ↓
cause Plugin is outdated or Prettier version mismatch.
fix
Update both prettier and prettier-plugin-kotlin to latest: npm install prettier@latest prettier-plugin-kotlin@latest
Warnings
breaking v2.0.0 replaced the JavaScript-based printer with a Kotlin-based one (kastree Writer). Output may differ significantly from v1.x. ↓
fix Reformat all Kotlin files with the new plugin version. Review diff of formatted files for regressions.
gotcha Parser (kotato) is alpha; may fail on complex Kotlin syntax (e.g., inline classes, contracts, multi-file inference). ↓
fix For production, use kotlinc or ktlint. Only use this plugin for experimental or simple projects.
gotcha VSCode Prettier extension may not auto-detect the plugin for Kotlin files; formatOnSave may not work despite manual formatting succeeding. ↓
fix Restart VSCode after installing plugin. Set 'prettier.requireConfig': false if needed. See https://github.com/prettier/prettier-vscode/issues/395
deprecated v1.x plugin used a different printer; upgrade to v2.x for current support. ↓
fix Update to version 2.1.0: npm install prettier-plugin-kotlin@latest
Install
npm install prettier-plugin-kotlin yarn add prettier-plugin-kotlin pnpm add prettier-plugin-kotlin Imports
- default plugin registration wrong
import prettierPluginKotlin from 'prettier-plugin-kotlin'correctprettier.config.js -> module.exports = { plugins: ['prettier-plugin-kotlin'] } - require (CommonJS) wrong
const prettierPluginKotlin = require('prettier-plugin-kotlin'); prettierPluginKotlin.format(code)correctconst prettier = require('prettier'); const code = prettier.format('fun main() {}', { parser: 'kotlin', plugins: [require('prettier-plugin-kotlin')] }) - ESM (type: module) wrong
import * as prettier from 'prettier'correctimport prettier from 'prettier'; const code = await prettier.format('fun main() {}', { parser: 'kotlin', plugins: ['prettier-plugin-kotlin'] })
Quickstart
// Ensure prettier and prettier-plugin-kotlin are installed
// npm install prettier prettier-plugin-kotlin
import prettier from 'prettier';
const code = `fun main(args: Array<String>) { println("Hello, World!") }`;
const formatted = await prettier.format(code, {
parser: 'kotlin',
plugins: ['prettier-plugin-kotlin'],
tabWidth: 2,
semi: false
});
console.log(formatted);
// Expected output:
// fun main(args: Array<String>) {
// println("Hello, World!")
// }