Skip to content
Webpack loader for Google Closure modules
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


npm deps test

This is a Webpack loader which resolves goog.provide() and goog.require() statements in Webpack as if they were regular CommonJS modules.


npm install --save-dev closure-loader


Documentation: Using loaders

NOTE: This loader is mainly meant for building (probably older) Closure Compiler projects with Webpack and to make a transition to other module systems (CommonJS or ES6) easier.

There are two parts to this loader:

  • goog.provide()
    • Basically just creates the given namespace in the local scope of that module
    • Any file containing this statement will be added to a map for require lookups
  • goog.require()
    • Like goog.provide() it creates the given namespace in the scope of that module
    • It finds the corresponding file with the goog.provide() statement and loads it (see configuration below)
    • It assigns the value of the namespace from the provide file and assign it to the same namespace in the current module

In the simplest way you can just use those two statements like you usually would with Google Closure Tools.

NOTE: Usually the Closure compiler simply creates all namespaces on the global scope (i.e. the window object). This is not the case if you use this loader. Every file ("module") has its own scope just like it would have if you used CommonJS syntax.

You can use Closure dependencies in conjunction with CommonJS syntax. You can load any module that uses goog.provide() with require(), but not the other way round.

// module.js
goog.provide(''); = function () {
    console.log('my module was loaded');

// index.js
var module = require('./module.js');

module(); // will output 'my module was loaded' to the console

ES6 Modules

If you use babel you can even use ES6 import syntax. If you have enabled the es6mode in the loader config the first goog.provide() of each file will be exported as "default" in addition to its full namespace.

// module.js
goog.provide(''); = function () {
    console.log('my module was loaded');

// index.js
import module from './module.js';
// is the same as
var module = require('./module.js').default;
// or
var module = require('./module.js');

module(); // will output 'my module was loaded' to the console


Here is an example Webpack config for this loader:

module.exports = {
    entry: {
        app: './src/index.js'
    output: {
        path: './build',
        filename: '[name].js'
    module: {
        rules: [
                test: /\/src\/.*\.js$/,
                loader: 'closure-loader',
                options: {
                    paths: [
                        __dirname + '/src',
                    es6mode: true,
                    watch: true,
                    fileExt: '.js',
                exclude: [/node_modules/, /test/]

Here are the configuration options specific for this loader:

  • paths (array): An array of path strings. The loader will search all *.js files within theses paths for goog.provide() statements when resolving a goog.require(). You can only goog.require() dependencies that can be found under one of these paths.
  • es6mode (boolean, default: false): If enabled it will add the value of the first goog.provide() as default export for usage with babel. For this reason it will also export the corresponding flag module.exports.__esModule = true
  • watch (boolean, default: true): If true, the loader will intitialise watchers which check for changes in the mapped files. This is neccesary to be able to delete the internal map cache. But it also makes problems with CI sytstems and build scripts, because the watcher will prevent the process from beeing exited.
  • fileExt (string, default: '.js'): Files extension which will be searched for dependency resolving. Support glob pattern syntax.

NOTE: This loader in no way includes or wraps the actual Google Closure Library. If you want to use the Closure Library you will have to include it yourself and ensure correct shimming:

module: {
    rules: [
            test: /google-closure-library\/closure\/goog\/base/,
            use: [
plugins: [
    new webpack.ProvidePlugin({
        goog: 'google-closure-library/closure/goog/base',


  • Steven Weingärtner - Original author & maintainer - eXaminator
  • Matt Mulder - Current maintainer - mxmul

See also the list of contributors who participated in this project.



You can’t perform that action at this time.