By Kevin Yank

Open repository brings modularity to JavaScript

By Kevin Yank
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

It’s early days, but a group of Perl hackers have banded together to form JSAN, The JavaScript Archive Network. Like the venerable Perl equivalent, CPAN, and other sites that borrowed inspiration (e.g. PEAR for PHP), JSAN aims to be an open repository for JavaScript scripts that are designed in a modular fashion so as to have manageable dependencies that encourage code reuse.

In practical terms, what this means is you can choose a particular module that provides functionality you’d like on your site, and the JSAN system will automatically download and install not only the files for that module, but also the files of any modules upon which that module depends. With the files installed in your site’s directory structure, you can then go ahead and use the functionality they provide in your own scripts.

As JSAN was conceived by Perl hackers, the automated system for downloading and installing JSAN modules is naturally written in Perl. If you’re a developer who works on a Windows box, I sympathise with that groan you just let out. If it’s any consolation, there are rumblings of an “alternative JSAN client” in the works, so fingers crossed. In the meantime, you need to install Perl and get familiar with your operating system’s command prompt before you can install and use JSAN.


Documentation is early-to-nonexistent at this stage, however anyone with some familiarity with CPAN or PEAR and a strong grasp of JavaScript should be able to figure it out based on the nascent installation guide that does exist. Documentation for using individual modules once installed is provided within the script files themselves in a standard format used on CPAN.

Aside from dependency tracking and auto-installation of scripts, JSAN places very few constraints on how scripts should be written. In fact, standalone modules can be obtained easily by browsing to the appropriate script file on the site and downloading it directly–no Perl client required. The initial library of modules, however, clearly favours unobtrustive scripting with JavaScript classes used as namespaces, as presented in DHTML Utopia.

If you do use the official client, you’ll benefit from a standardized directory structure, which will let you use JSAN’s built-in import mechanism for scripts. A trick I first saw in the jsolait library for XML-RPC, the system allows a script to load another script that it requires on-the-fly, instead of requiring the Web developer to load that script with a separate

All technical rambling aside, what this means is that if your script requires the List.Utils CPAN module, you can load it automatically if necessary using this code in your script:

try {
if (!List.Utils) JSAN.use('List.Utils');
} catch (e) {
throw 'This script requires the List.Utils module from JSAN.';

The nitty gritties are documented in the burgeoning JSAN wiki.

One tasty nugget on the wiki is a list of browser compatibility issues to consider when writing scripts for JSAN. Some deliciously obscure gotchas to be found there, like the fact that "true" and "false" (the strings) both evaluate to true in JavaScript.

Also worthwhile following is Planet JavaScript, an aggregation of a number of top JavaScript blogs along with JSAN module releases.

Login or Create Account to Comment
Login Create Account
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?