Helma Logo
main list history

Module Loader

Module Path Setup

Helma NG loads JavaScript resources using a module loader that is compliant with the ServerJS Securable Modules proposal:

https://wiki.mozilla.org/ServerJS/Modules/SecurableModules

Helma NG actually goes one step further and makes sure every module has its own top level scope, so modules are fully isolated from each other, providing a programming environment that resembles that of Python environment more than the one of ordinary client-side JavaScript runtime.

Helma uses the concept of a module path to look up and load modules that is similar to the PATH environment variable used to find executables on most operating systems. By default, the module path consists of two entries:

  1. The application root, which is the parent directory of the command line script, or the current working directory if called without script argument.

  2. The system modules root, which corresponds to the modules directory in the Helma NG home directory.

Helma NG provides several ways to access and set the module path. The simplest is to set the HELMA_MODULE_PATH environment variable, separating multiple entries with ':' or whatever character is used to separate PATH entries on your system:

 export HELMA_MODULE_PATH=../foo/lib:../my/lib

Alternatively, you can define the module path using the helma.modulepath Java system property, and you can add entries to the module path using the addRepository() method in the helma/system module.

Module and Resource Loading

Helma NG provides three functions with different semantics to load modules:

require(moduleName)

The require function provides the functionality defined in the ServerJS Securable Modules proposal. It tries to locate a module in the module path, loads it and returns its exports object.

import(moduleName)

The import function builds on top of require, additionally setting a property in the calling module scope whose name is the name of the loaded module and whose value is the loaded module's exports object.

include(moduleName)

The include function builds on top of require, additionally copying all exported properties of the loaded module to the calling module scope.

export(propertyName[, ...])

The export function provides an alternative method to the exports object to define exported properties in a module by passing the names of exported properties as arguments.

addToClasspath(pathName)

This function adds a jar file or directory to the classpath. By default, all jar files in the Helma NG lib directory are included in the classpath.

getResource(pathName)

This looks for a file with the given path name in the module path and returns a resource object. This can be used to load resources other than JavaScript files using the same lookup rules as the module loader.