I've been working on code that works on Browser, Web Workers and NodeJS. In order to export my module, I've been writing ugly code like this one:

(function () {
  /* ... Code that defines MyModule ... */
 
  var all;
  if (typeof self !== 'undefined') {
    all = self; // Web Worker
  } else if (typeof window !== 'undefined') {
    all = window; // Browser
  } else if (typeof global !== 'undefined') {
    all = global; // NodeJS
  }
  all.MyModule = MyModule;
 
  if (typeof module !== 'undefined') {
    module.exports = MyModule;
  }
})();

One-line Solution

Guillaume Marty showed me that sink.js uses this as a replacement for self, window and global. I managed to add support for module.exports in a one-liner!

(function (global) {
  /* ... Code that defines MyModule ... */
 
  global.MyModule = (global.module || {}).exports = MyModule;
})(this);

I have been looking for this magic line for a long time, I hope it will be useful to you too :)

If you liked this article, you might be interested in my Twitter feed as well.
 
  • Nico

    The pitfall (or the advantage, depends on what your primary intention was) in your solution is that you assume that your parameter "this" will be the global object.
    Which he may not be depending on the context.
    Your code will be more robust if you retrieve the global object this way :

    global = (function(){ return this || (1,eval)('this') })();

    I suggest you may read this article :
    http://perfectionkills.com/unnecessarily-comprehensive-look-into-a-rather-insignificant-issue-of-global-objects-creation/

    Joyeux noël !
    Nico.

  • Vjeux .

    My goal isn't to have a bullet-proof version involving weird tricks such as indirect eval calls but something that work under normal conditions for Browser, WebWorkers and NodeJS.

    Thanks for the link, re-reading it from times to times can't harm :)

 

Random Posts

  • August 4, 2009 -- Project – Fooo (1)
    [caption id="attachment_377" align="alignright" width="96" caption="hgf.fooo.fr"][/caption] Presentation Fooo is my first year school project written in Delphi. During 8 months, Vladimir Nachbaur, Alban Perillat-Merceroz, Felix Abecassis and I developed a game that mimics Warcraft III and achi...
  • January 22, 2010 -- Project – WoW Genuine (0)
    [caption id="attachment_988" align="alignright" width="175" caption="WoW Genuine"][/caption] Working on the World of Raids Guild Recruitment I needed to make sure the user was really member of the guild he claimed to represent. Since there is no official API and we don't want to ask for the user ...
  • September 17, 2011 -- WoW Interface Anchor Positioning (3)
    I've always found CSS positioning with both float and position: absolute/relative hard to work with. I want to introduce to you an alternative way borrowed from the World of Warcraft Interface: Anchors. Anchor The concept is extremely simple. You can tell where you want the element to be, relati...
  • October 5, 2011 -- Javascript Presentation (1)
    The talk is over. Check out the Slides & Video. For several months now I've been surveying my friends and teachers at EPITA and I came to the conclusion that they have absolutly no idea what Javascript really is. In order to help them discover a language that is getting a lot of traction nowa...
  • March 22, 2010 -- Project – SC2Mapster (0)
    [caption id="attachment_1060" align="alignright" width="200" caption="SC2Mapster"][/caption] Let's get back three months before the beginning of my internship. Blizzard just released the private beta of the long waited real-time strategy (RTS) game Starcraft 2, a remake of their first extreme...