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

JavascriptCommonjs

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionClay NicholsView Question on Stackoverflow
Solution 1 - JavascriptRayViljoenView Answer on Stackoverflow