Registry / devops / prettier-plugin-java-for-cjs

prettier-plugin-java-for-cjs

JSON →
library2.8.4jsnpmunverified

Prettier plugin that adds support for formatting Java code. Current stable version is 2.8.4, released with a maintenance cadence. It uses java-parser (Chevrotain-based) to parse Java into a CST and applies Prettier's formatting rules. Key differentiators: integrates seamlessly with Prettier CLI and API, supports custom entrypoints for formatting code snippets, and enforces consistent Java code style across projects. Compatible with Prettier 3.x only.

npm install prettier-plugin-java-for-cjs
INSTALL
IMPORT
SIG · PRETTIER-PLUGIN-JA
P
prettier-plugin-java-for-cjs
devopsjavascriptv2.8.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
const prettier = require('prettier'); const formatted = prettier.format(code, { parser: 'java' });
import prettier from 'prettier'; formatted = prettier.format(code, { parser: 'java' });
Plugin auto-loads when installed; no explicit import of the plugin is needed. The example uses CommonJS require as shown in docs.
plugin as module
import * as prettierPluginJava from 'prettier-plugin-java';
import prettierPluginJava from 'prettier-plugin-java';
The plugin does not have a default export; use namespace import. Typically you don't need to import it directly.
CLI usage
prettier --write MyFile.java
prettier --parser java --write MyFile.java
Plugin auto-detects .java files. The --parser flag is not required and may cause issues if the plugin is not loaded.

Showcases formatting a simple Java class using Prettier API with the java parser.

const prettier = require('prettier'); const javaCode = `public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }`; prettier.format(javaCode, { parser: 'java' }).then(result => console.log(result));
prettier --version
Debug
Known issues
breakingprettier-plugin-java v2.x requires Prettier v3.x. Using Prettier v2 will fail to load the plugin.
fix
Upgrade Prettier to ^3.0.0. Run: npm install prettier@^3.0.0 --save-dev
affects: >=2.0.0
deprecatedPrettier v2 support dropped; v1.x of this plugin is no longer maintained.
fix
Upgrade to prettier-plugin-java@^2.0.0 and Prettier@^3.0.0
affects: <=1.6.2
gotchaPlugin must be installed locally or globally to be auto-loaded by Prettier. If not, you need to pass --plugin path explicitly.
fix
Install globally: npm install -g prettier-plugin-java, or reference plugin via --plugin=./node_modules/prettier-plugin-java
affects: >=2.0.0
gotchaCustom parser option 'entrypoint' only works with certain node types; invalid entrypoint causes formatting to fail silently.
fix
Use one of the valid entrypoints listed in the plugin's options file.
affects: >=2.0.0
Errors
Common errors & fixes
Error: Cannot find module 'prettier-plugin-java'
Plugin not installed or not in node_modules when using require/import.
fix
Run npm install prettier-plugin-java --save-dev
Error: Couldn't resolve parser "java"
Plugin not loaded or Prettier version incompatible (v2.x).
fix
Ensure prettier-plugin-java@^2.0.0 with Prettier@^3.0.0, and install the plugin.
Error: Invalid entrypoint 'myEntry' for java parser
Entrypoint option value not in allowed list.
fix
Check the options file for valid entrypoints; use one like 'compilationUnit' or 'classBodyDeclaration'.
Upgrade
Version history
2.8.4latest on npm
Audit
Dependencies
prettierrequiredPeer dependency required as the plugin runs within Prettier
Agent activity
2 hits · last 30 days
node
2
Resources
prettier-plugin-java-for-cjs — npm install prettier-plugin-java-for-cjs · libregistry