My first question is : Who is responsible to handle require statements in a Nodejs application ? is it Node itself ? or CommonJS ? or RequireJS ? Is CommonJS included in Node ? what about RequireJS?
Now my second question :
I have an if-else statement , which decides if we are rendering on server side or client side. I want to load different libraries when it's rendering on client side or server side. Is it possible to load the modules in runtime ? exactly at the moment it's required ?
if (typeof window === undefined){
var serverSideLibrary = require('A');
//....
}else{
var clientSideLibrary = require('B');
}
It looks like Node loads everything required before starting the application. So it's not important if you require it at the top of the code or in that if-else block.
require(), but have a level of similarity akin to Java and JavaScript -- really only in the name. And, CommonJS is a collection of specifications, including for how arequire()function should behave. Node.js would be an implementation of that spec.