Asynchronous Require
You can use the asynchronous (array) version of require require(['...'], function(){...})
, anywhere you like, Web/AMD or nodejs (and of course UMD, <script/> etc.
Keep in mind that the asynchronous require is essentially the only proper way to conditionally load 'myHugeButOptionalModule'
on the Web/AMD side. For instance,
if (true) {// perhaps some code here,
// to conditionally call asynch 'require' bellow
require(['depdir/dep1', 'depdir/dep2'], function(dep1, dep2) {
// module factory function
// called asynchronously & after dep1 & dep2 are loaded
// module returned here
});
}
// code here is executed immediately after calling `require`,
// BEFORE calling the factory function, since its asynchronous.
The asynch require always runs asynchronously on nodejs, just like it does on Web RequireJS/AMD.
Note: versions of RequireJS < 2.1.x were not consistent on the asynchronous call of require(['dep1', 'dep2'], fn): if all your dependencies ['dep1', 'dep2'] had already been loaded/cached before, the call to fn was actually synchronous. uRequire now matches the behaviour of the latest RequireJS (always asynch).