google-closure-compiler-java

raw JSON →
20260428.0.0 verified Fri May 01 auth: no javascript

Java platform distribution of Google's Closure Compiler, a JavaScript optimizer and minifier that produces highly efficient code. This package wraps the Closure Compiler Java library for use in Node.js environments. Current stable version is 20260428.0.0, released weekly alongside upstream Closure Compiler versions. Key differentiators: full Closure Compiler functionality (type checking, optimization, dead code elimination) via Java runtime; suitable for build pipelines; no GraalVM native-image dependency unlike the native platform packages. Compares to other minifiers (Terser, UglifyJS) by offering advanced compilation passes, strict type checking, and cross-module optimization.

error Error: java not found. Please install Java and add it to your PATH.
cause Java runtime is not installed or not accessible on system PATH.
fix
Install Java 8+ (e.g., from adoptopenjdk.net) and ensure 'java -version' works in terminal.
error Error: Cannot find module 'google-closure-compiler-java'
cause Package not installed or import path wrong.
fix
Run 'npm install google-closure-compiler-java' and use correct import: import compiler from 'google-closure-compiler-java'.
error TypeError: compiler is not a function
cause Using default import but expecting compile function; default is the module namespace.
fix
Use import { compile } from 'google-closure-compiler-java' or import compiler from ... then compiler.compile().
breaking Requires Java 8 or later runtime installed; no bundled JRE.
fix Install Java Runtime Environment (JRE) 8+ and ensure java is on PATH.
gotcha The package does not include a Java binary; it only provides Node.js wrapper to invoke system Java. Misunderstanding expects bundled Java.
fix Use google-closure-compiler which includes platform-specific native images if Java not desired.
deprecated The 'compile' function's 'jsOutputFile' property removed in v20221104 to discourage file system side effects.
fix Use in-memory compilation and write output manually if needed.
gotcha compilationLevel must be a string matching Closure Compiler API; wrong case or misspelling fails silently.
fix Use exact values: 'WHITESPACE_ONLY', 'SIMPLE', 'ADVANCED'.
gotcha Error handling: compile returns warnings and errors in result object; does not throw on compilation failure.
fix Always check result.errors array length before using result.code.
npm install google-closure-compiler-java
yarn add google-closure-compiler-java
pnpm add google-closure-compiler-java

Demonstrates basic usage: compile a simple JavaScript string with advanced optimizations, handling errors.

import { compile } from 'google-closure-compiler-java';
const { code, warnings, errors } = compile({
  jsCode: [{ src: 'function hello(name) { return "Hello, " + name; }' }],
  compilationLevel: 'ADVANCED',
  languageIn: 'ECMASCRIPT_2021',
  languageOut: 'ECMASCRIPT5'
});
if (errors.length) {
  console.error('Errors:', errors);
} else {
  console.log('Compiled JS:', code);
}