RequireJS is not require()

Written by - Saumya

05 December 2015

It would have saved me time if I could have known this in the beginning. Like a lot of things in Javscript world, a same kind of name for two different functionalities makes a lot of confusion.

RequireJS is used to load AMD
require() is used to load CommonJS

Well, to add to confusion the require() function is available to both the module definitions. But both the require() do different things. For CommonJS require() loads modules in a synchronous way, while the require() in AMD ie; in RequireJS the modules are loaded in asynhronous way.

AMD style loading is

require(['path/myModuleFileName.js'],function(MyModule){
  // Use MyModule
  MyModule.doSomething();
});

In CommonJS its done as below

var MyModule = require('path/myModuleFileName.js');
// the line below is same as above, just removed the file extention .js
//var MyModule = require('path/myModuleFileName');

Happy requiring.