Using modules in google closure compiler

1.8k views Asked by At

I'm just trying to get a basic module working:

testA.js:

goog.module('foo');

exports.bar = function(){
  console.log('here');
};

testB.js:

var bar = goog.require('foo.bar');
bar();

And I'm running:

java -jar ~/Downloads/compiler.jar --js testA.js testB.js

which yields:

testB.js:1: ERROR - Closure dependency methods(goog.provide, goog.require, etc) must be called at file scope.
var bar = goog.require('foo.bar');
          ^

1 error(s), 0 warning(s)

Any advice?

1

There are 1 answers

2
AudioBubble On

Answering this myself. So you can't require modules normally from a "normal" closure compiled .js file, only from a module file, which seems to be defined as any file that has goog.module in it.

So If in a module, goog.require('module') works fine. If not in a module, goog.module.get('module') is needed