closure-compiler

raw JSON →
0.2.12 verified Fri May 01 auth: no javascript maintenance

Node.js wrapper for Google's Closure Compiler Java tool. v0.2.12 – stable but infrequent releases, last updated 2019. Runs the Closure Compiler JAR in a child process and returns compiled JavaScript via callback. Provides a simple API for advanced compilation and dead code elimination, but requires Java runtime. Alternatives like google-closure-compiler offer more frequent updates.

error Error: Command failed: java -jar <path> ... exit code 1
cause Closure Compiler JAR not found or Java not installed.
fix
Ensure java is installed and closure-compiler.jar is bundled.
error TypeError: compile is not a function
cause Incorrect import of default vs named export.
fix
Use named import: import { compile } from 'closure-compiler'
error Error: Cannot find module 'closure-compiler'
cause Package not installed or not in node_modules.
fix
Run npm install closure-compiler
gotcha Requires Java installed on PATH to run the Closure Compiler JAR.
fix Install Java Runtime Environment (JRE) and ensure java is in PATH.
deprecated This package uses callbacks; no Promise support natively.
fix Wrap compile() in a Promise manually or use a wrapper.
gotcha Options are passed as command-line flags; array values become repeated flags.
fix For array options, set as array in options object; the wrapper handles repetition.
npm install closure-compiler
yarn add closure-compiler
pnpm add closure-compiler

Compile a JavaScript file with advanced optimizations using Closure Compiler asynchronously.

import { compile } from 'closure-compiler';
import { readFileSync } from 'fs';

const code = readFileSync('input.js', 'utf8');
compile(code, { compilation_level: 'ADVANCED_OPTIMIZATIONS' }, (err, stdout, stderr) => {
  if (err) {
    console.error('Compilation error:', stderr);
    process.exit(1);
  }
  console.log(stdout);
});