Working with modules

  1. Loading and using ES Modules from API V3
  2. Implementing and using custom ES Modules

Loading and using ES Modules from API V3

The embedded Graal.JS scripting engine can import ES Modules.

Here’s an example scripts which loads the built in module josm/util.

import * as util from 'josm/util'
util.println('Hello world!')

// or:
import {println} from 'josm/util' 
println('Hello World!')

Implementing and using custom ES Modules

You can implement and use your own ES Modules.

Here’s an example of a simple module that exports the function sayHello().

// file: helloworld.js
export function sayHello() {
    const System = Java.type('java.lang.System')
    System.out.println("Hello world!")
}

A client script can load and use the module. Here’s an example:

import {sayHello} from 'helloworld'
sayHello()

In the JOSM preferences, you can configure where the scripting plugin is looking for modules. The plugin looks for modules in two places:

  • First, it tries to load modules from the plugin jar file in the /js/v3 directory.

  • Then, it tries to load it from one of the configured module repositories. Each repository is either a directory in the local file system or in a jar/zip file in the local file system.