Commons JS – Improving Client-Side JS by Improving Server-Side JS

Although CommonsJS is a set of standard APIs targeted at server-side JavaScript, it is adding value to client-side JavaScript by addressing low-level weaknesses in the language that have not yet been included in the ECMAScript standard.

For example, JavaScript has no mechanism for defining modules, which has led to library developers exposing APIs through variables in the global namespace – risking collisions when two library developers choose the same variable. To solve this problem, the CommonsJS Modules API defines a way for module developers to safely export an API to clients. This API has inspired the Asynchronous Module Definition API, a browser-friendly version of the API that provides for loading modules asynchronously, on-demand over HTTP.

The Commons Packages API enables developers to define metadata for a module in a consistent way (through a package.json file), for example the name, description and version of the package. More importantly, it enables the author to declare dependencies on other packages, which paves the way for automated package management. Node’s package manager (npm) is widely used for server-side packages but package managers for client-side libraries also exist (e.g. VoloJS and JamJ). These enable client-side developers to more easily manage the large number of 3rd party libraries needed for large-scale web development.

As a final example, JQuery developers (since version 1.5) have been using the Deferred object, which is returned from jQuery.ajax(). The Deferred object is based on the CommonsJS Promises/A specification, which in turn was inspired by the Deferred object in Dojo. Having a common API for registering callbacks for asynchronous functions is a benefit because different frameworks will have common approaches to AJAX calls, which makes it easier for developers to learn the framework more quickly.

While CommonsJS may seem mundane compared to the exciting UI libraries emerging from the client-side JavaScript community, the solid foundations it provides will ultimately help client-side developers make their libraries consistent, modular and interoperable.