closure-compiler-service

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

A Node.js wrapper for the Google Closure Compiler Service API that compiles JavaScript remotely without a Java dependency. Current stable version is 0.6.1. Release cadence is low; the library is minimal and stable. Key differentiators: no Java runtime required unlike the official closure-compiler Java implementation, simple API with both command-line and programmatic usage. Alternatives include google-closure-compiler (Java-based) and terser.

error Error: connect ECONNREFUSED
cause Closure Compiler Service API endpoint unreachable due to network or server down.
fix
Check internet connection or retry later.
error TypeError: input is not a string or Buffer
cause js_code argument passed as object or number.
fix
Convert input to a string or Buffer before passing.
error Error: Invalid compilation_level '...' provided
cause An unsupported compilation level string was passed.
fix
Use one of: 'WHITESPACE_ONLY', 'SIMPLE_OPTIMIZATIONS', 'ADVANCED_OPTIMIZATIONS'.
gotcha Default options set output_format to 'json'; if changed to something else, the callback may receive unexpected data.
fix Leave output_format as 'json' or handle non-JSON response manually.
gotcha The compile function expects a String or Buffer; passing an object or other type may cause errors.
fix Ensure js_code is a string or Buffer; use .toString() if needed.
gotcha Network errors (e.g., service unavailable) are not handled gracefully; errors from the API are passed to callback but connection failures may throw.
fix Wrap call in try-catch for network failures or use a timeout.
npm install closure-compiler-service
yarn add closure-compiler-service
pnpm add closure-compiler-service

Minifies JavaScript via Google Closure Compiler Service API using both default and advanced options.

const ccs = require('closure-compiler-service');
const fs = require('fs');

const input = fs.readFileSync('input.js', 'utf8');
ccs.compile(input, function(errs, warns, code) {
  if (errs) {
    console.error('Errors:', errs);
    return;
  }
  console.log(code); // compiled minified code
});

// With options:
ccs.compile(input, {
  compilation_level: 'ADVANCED_OPTIMIZATIONS'
}, function(errs, warns, code) {
  if (errs) {
    console.error('Errors:', errs);
    return;
  }
  console.log(code);
});