Naming a Custom Module

You can assign a custom module name to facilitate reuse. Once configured, the module can be required without specifying the full path.

Naming modules also improves compatibility with third-party libraries and helps prevent naming conflicts.

To do this, use `define(id, [dependencies,] moduleObject)` and set up a `require` function accordingly.

  1. Create your custom module file and upload it to the File Cabinet. For example, create a JavaScript file containing the following code and name it math.js, then save it in the SuiteScripts/Example folder in the File Cabinet.
...
let myMath = {
    add: function(num1, num2) {
        return num1 + num2
    },
    subtract: function(num1, num2) {
        return num1 - num2
    },
    multiply: function(num1, num2) {
        return num1 * num2
    },
    divide: function(num1, num2) {
        return num1 / num2
    },
}
...

  1. Copy
  2. To load the custom module by name, specify the module file name (alias) and its path by configuring the paths parameter in a JSON file (for example, myconfig.json).
...
{
    "paths": {
        "math": "/SuiteScripts/Example/math.js"
    }
    "shim":{
        "math": {
            "exports": "myMath"
        }
    }
}
...

  1. Copy
  2. Load the custom module in your SuiteScript 2.x script by including the @NAmdConfig JSDoc tag with your .json file name and passing the custom module name to the define Object.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NAmdConfig  ./myconfig.json
*/

define(['math'], function (math) {
    return {
        beforeLoad: function beforeLoad() {
            log.debug({
                title: 'test',
                details: myMath.add(1,2)
            });
        }
    }
});

Leave a comment

Your email address will not be published. Required fields are marked *