Skip to content

module

The current module.

Scriptable treats each file as a module. Consider the following file.

let circle = importModule('circle')
let r = 2
let area = circle.area(r)
log('Area of circle: ' + area)

The file imports the module circle.js which has the following contents.

module.exports.area = (r) => {
  return Math.PI * Math.pow(r, 2)
}

module.exports.circumference = (r) => {
  return 2 * Math.PI * r
}

The circle.js module exports the functions area and circumference. You can add any function or object to the exports of a module to make them available when the module is imported with importModule.

filename

Path to file containing the module.

Read-only.

This is the absolute path to the file containing the module.

filename: string

exports

Exported functions and modules.

Values assigned to exports are returned by the global importModule function when the module is imported.

exports can be of any type but by default it is an empty object. Consider the following example which exports the area and circumference functions.

module.exports.area = (r) => {
  return Math.PI * Math.pow(r, 2)
}

module.exports.circumference = (r) => {
  return 2 * Math.PI * r
}

Alternatively if you only need to export a single function or object, you can assign directly to the exports property as shown in the following examples.

module.exports = (r) => {
  return 2 * Math.PI * r
}

module.exports = "My string"

exports: any