title: Node-style require for in-browser javascript?
body: <p>Are there any libraries for in-browser javascript that provide the same flexibility/modularity/ease of use as Node's <code>require</code>?</p>
  <p>To provide more detail: the reason <code>require</code> is so good is that it:</p>
  <ol>
  <li>Allows code to be dynamically loaded from other locations (which is stylistically better, in my opinion, than linking all your code in the HTML)</li>
  <li>It provides a consistent interface for building modules</li>
  <li>It is easy for modules to depend on other modules (so I could write, for instance, an API that requires jQuery so I can use <code>jQuery.ajax()</code></li>
  <li>Loaded javascript is <em>scoped</em>, meaning I could load with <code>var dsp = require(&quot;dsp.js&quot;);</code> and I would be able to access <code>dsp.FFT</code>, which wouldn't interfere with my local <code>var FFT</code></li>
  </ol>
  <p>I have yet to find a library that does this effectively.  The workarounds I tend to use are:</p>
  <ul>
  <li><p><a href="https://github.com/fairfieldt/coffeescript-concat" rel="noreferrer">coffeescript-concat</a> -- it's easy enough to require other js, but you have to compile it, which means it is less great for fast development (e.g. building APIs in-test)</p>
  </li>
  <li><p><a href="http://requirejs.org/" rel="noreferrer">RequireJS</a> -- It's popular, straightforward, and solves 1-3, but lack of scoping is a real deal-breaker (I believe <a href="http://headjs.com/" rel="noreferrer">head.js</a> is similar in that it lacks scoping, though I've never had any occasion to use it.  Similarly, <a href="http://labjs.com/" rel="noreferrer">LABjs</a> can load and <code>.wait()</code> does mollify dependency issues, but it still doesn't do scoping)</p>
  </li>
  </ul>
  <p>As far as I can tell, there appear to be many solutions for dynamic and/or async loading of javascript, but they tend to have the same scoping issues as just loading the js from HTML.  More than anything else, I would like a way to load javascript that does not pollute the global namespace at all, but still allows me to load and use libraries (just as node's require does).</p>
  <p><strong>2020 UPDATE:</strong> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules" rel="noreferrer">Modules</a> are now standard in ES6, and as of mid-2020 are natively supported by <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#Browser_support" rel="noreferrer">most browsers</a>. Modules support both synchronous and asynchronous (using Promise) loading. My current recommendation is that most new projects should use ES6 modules, and use a transpiler to fall back to a single JS file for legacy browsers.</p>
  <p>As a general principle, bandwidth today is also typically much wider than when I originally asked this question. So in practice, you might reasonably chose to always use a transpiler with ES6 modules, and focus your effort on code efficiency rather than network.</p>
  <p><strong>PREVIOUS EDIT (or if you don't like ES6 modules):</strong> Since writing this, I have extensively used <a href="http://requirejs.org/" rel="noreferrer">RequireJS</a> (which now has much clearer documentation). RequireJS really was the right choice in my opinion. I'd like to clarify how the system works for people who are as confused as I was:</p>
  <p>You can use <code>require</code> in everyday development. A module can be anything returned by a function (typically an object or a function) and is scoped as a parameter. You can also compile your project into a single file for deployment using <code>r.js</code> (in practice this is almost always faster, even though <code>require</code> can load scripts in parallel).</p>
  <p>The primary difference between RequireJS and node-style require like browserify (a cool project suggested by tjameson) uses is the way modules are designed and required:</p>
  <ul>
  <li>RequireJS uses AMD (Async Module Definition). In AMD, <code>require</code> takes a list of modules (javascript files) to load and a callback function. When it has loaded each of the modules, it calls the callback with each module as a parameter to the callback. Thus it's truly asynchronous and therefore well-suited to the web.</li>
  <li>Node uses CommonJS. In CommonJS, <code>require</code> is a blocking call that loads a module and returns it as an object. This works fine for Node because files are read off the filesystem, which is fast enough, but works poorly on the web because loading files synchronously can take much longer.</li>
  </ul>
  <p>In practice, many developers have used Node (and therefore CommonJS) before they ever see AMD. In addition, many libraries/modules are written for CommonJS (by adding things to an <code>exports</code> object) rather than for AMD (by returning the module from the <code>define</code> function). Therefore, lots of Node-turned-web developers want to use CommonJS libraries on the web. This is possible, since loading from a <code>&lt;script&gt;</code> tag is blocking. Solutions like browserify take CommonJS (Node) modules and wrap them up so you can include them with script tags.</p>
  <p>Therefore, if you are developing your own multi-file project for the web, I strongly recommend RequireJS, since it is truly a module system for the web (though in fair disclosure, I find AMD much more natural than CommonJS). Recently, the distinction has become less important, since RequireJS now allows you to essentially use CommonJS syntax. Additionally, RequireJS can be used to load AMD modules in Node (though I prefer <a href="https://github.com/ajaxorg/node-amd-loader" rel="noreferrer">node-amd-loader</a>).</p>