Narwhal on Persevere

By on September 1, 2009 1:53 pm

Narwhal is an open source toolkit for server-side JavaScript that delivers a growing library of useful modules implementing the CommonJS standard library API. CommonJS is an effort to provide a standard library for server side JavaScript (or JavaScript in any privileged environment) and Narwhal is the most extensive implementation of the library API. Narwhal provides numerous system level functions and IO capabilities with streaming and File interaction.

Persevere is a framework for server-side JavaScript with object persistence and direct mapping to RESTful HTTP interaction. These two projects can complement each other very nicely and since Persevere implements the CommonJS API for module loading, Narwhal’s standard library of modules can now easily be utilized within Persevere.

Making Narwhal modules available in Persevere is very simple. Download Narwhal, and then add Narwhal’s lib directories for the Rhino platform to the set of paths for the module loader:


Narwhal also expects its global module to be run prior to any other modules running in order to augment the native constructors with certain EcmaScript 5 additions:


You can now add this code to one of your Persevere jslib modules (put it in a .js file, and put it in your WEB-INF/jslib directory) and start using Narwhal modules within Persevere:

var file = require("file"); // load a Narwhal module

For portability of your application, you might want to put the Narwhal package into your Persevere application directory. It is recommended that you put it in the WEB-INF directory unless you want the Narwhal files to be available to web clients. You could then reference the lib directories with relative paths:


Narwhal provides a powerful set of standard library functions with extensive File and I/O capabilities. With the interoperability provided by CommonJS, Persevere and Narwhal make an excellent combination.