What is CommonJS and why should I care how it can help in writing .js applications?

Clay Nichols picture Clay Nichols · Feb 28, 2013 · Viewed 27.2k times · Source

I'm learning JS and someone more experienced mentioned that they use CommonJS to handle keeping js code organized (since there's no "module" functionality).

I looked on Quora, but it explained the group and it's goals, not so much how I might use it.

Answer

RayViljoen picture RayViljoen · Feb 28, 2013

CommonJS modules enables you to include javascript modules within the current scope and effectively keeps the global scope from being polluted.

This massively reduces the chance of naming collisions and keeps code organised.

That said I think it's pretty safe to say that AMD modules have now become more popular than CommonJS.

The RequireJS website has a very good section on why to use modules and the differences between CommonJS and AMD.