goog.provide() under whitespaceonly optimizations doesn't work under node.js

71 views
Skip to first unread message

Kevin Lynagh

unread,
Feb 11, 2012, 2:21:54 AM2/11/12
to Closure Library Discuss
If I understand correctly,

goog.provide ensures that objects exist so you can write:

goog.provide('a.b.c')
a.b.c = 17

and objects a and b will exist and the assignment will work fine.

Running node.js on base.js with those two lines appended to the end
throws an error; "a not defined".
The same script works fine if you load it into a browser.


Is this the expected behavior, or a bug?
From what I could tell looking at how goog.provide works, it seems
like just JavaScript and not browser/DOM specific at all.

Michael Bolin

unread,
Feb 14, 2012, 2:34:17 AM2/14/12
to closure-lib...@googlegroups.com
The last time I looked at this, the problem was that Node does not like how goog.require() creates the global object "a" in your example.

To this end, I modified base.js slightly in plovr such that goog.provide() works in Node:


though it requires that you specify a --define "goog.NODE_JS=true" (see the commit message that I linked to for more details).

I haven't tried to upstream my change to Closure Library because I'm not convinced that it's the best way to do Node+Closure yet (though it is the best one that I have found).

I posted a more detailed response on StackOverflow:


Though I'm a bit annoyed that the "Don't use Closure Compiler" is ranked above my answer...

Nick Santos

unread,
Feb 14, 2012, 10:43:13 AM2/14/12
to closure-lib...@googlegroups.com

To be fair, I think "don't use the output of Closure Compiler on
Node.js" is a totally reasonable answer.

A while back, we tossed around the idea of having pluggable boot
loaders for different environments (see the code under
bootstrap/webworkers.js for a possible solution), but i agree with
Bolin that there's not really a satisfying answer to this yet.

Reply all
Reply to author
Forward
0 new messages