Google Closure Library
Google Closure Library is a comprehensive, low-level JavaScript library developed by Google for building complex, highly performant, and scalable web applications. It serves as the foundation for many of Google's own web properties, including Gmail, Google Docs, and Google Maps. The library is not a typical npm package with standard ES module exports but rather provides its own module system (`goog.require`, `goog.provide`) built around a global `goog` object. It ships with a vast array of utilities covering DOM manipulation, UI components, networking, internationalization, data structures, and more. The current stable version is 20230802.0.0, and it follows a roughly monthly to bi-monthly release cadence, providing continuous updates and improvements, often tied to internal Google development cycles. Its key differentiators include its robust type system (when used with Closure Compiler), extensive cross-browser compatibility layers, and a strong emphasis on performance and security, making it suitable for large-scale enterprise applications.
Common errors
-
ReferenceError: goog is not defined
cause The base Closure Library file (and thus the global `goog` object) has not been loaded or initialized.fixIn Node.js, ensure you have `require('google-closure-library');` at the top of your script. In a browser, ensure `base.js` is loaded via a `<script>` tag before any code that uses `goog`. -
Error: 'goog.some.Component' not found
cause The specified Closure Library component has not been loaded using `goog.require`, or there's a typo in the component path, or the component doesn't exist.fixVerify the component name and path. Ensure `goog.require('goog.some.Component');` is called before attempting to use the component. If using the Closure Compiler, ensure the component is part of your compiled output. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax with Closure Library components, which are not exposed as standard ES modules.fixUse the Closure Library's native `goog.require()` system after loading the base library via CommonJS `require('google-closure-library');` in Node.js, or a script tag for `base.js` in browsers.
Warnings
- breaking The debug loader no longer downlevels sources in the browser, and `transpile.js` has been deleted. This affects development workflows that relied on in-browser transpilation.
- breaking Support for Internet Explorer 10 (IE10) has been officially dropped.
- deprecated `SafeHtml.create`, `SafeHtml.createScript`, `SafeHtml.setObjectData`, and `SafeHtml.setFrameSrc` methods are deprecated due to security considerations and to encourage safer templating practices.
- breaking The `defaultImpl` parameter has been removed from Closure Delegates helper methods, which may break existing delegate implementations.
- breaking The `closure/html` types' constructors have been made private, preventing direct instantiation.
- deprecated `goog.dom.safe.setImageSrc` has been deleted.
Install
-
npm install google-closure-library -
yarn add google-closure-library -
pnpm add google-closure-library
Imports
- goog
import goog from 'google-closure-library';
require('google-closure-library'); - goog.crypt.Sha1
import { Sha1 } from 'google-closure-library/goog/crypt/sha1';require('google-closure-library'); goog.require('goog.crypt.Sha1'); - goog.ui.Dialog
const Dialog = require('google-closure-library/goog/ui/dialog');require('google-closure-library'); goog.require('goog.ui.Dialog');
Quickstart
require('google-closure-library');
goog.require('goog.crypt.Sha1');
goog.require('goog.array');
const sha1 = new goog.crypt.Sha1();
const data = 'Hello, Closure Library!';
sha1.update(data);
const hash = sha1.digest();
console.log(`SHA-1 hash of '${data}': ${goog.array.map(hash, byte => byte.toString(16).padStart(2, '0')).join('')}`);
// Example of a utility that might not be available directly in Node.js
// goog.require('goog.dom');
// console.log(goog.dom.getWindow()); // This would typically only work in a browser environment