Naming a Custom Module

You can set up a custom module name to make re-use easier. Once you configure the name, you can require the module without worrying about its path.

Loading custom modules by name also helps you use third-party libraries and may help avoid naming conflicts.

You’ll need to call define(id, [dependencies,] moduleObject) and set up a require Function.

To load a custom module by name:

For more about the @NAmdConfig JSDoc tag, see require Configuration.

  1. Create your custom module file and upload it to the File Cabinet. For example, make a math.js file with your code and save it in SuiteScripts/Example.

                    ...
    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
        }, 
    }
    ... 
    
                  
  2. To load your module by name, add the module’s file name (alias) and its path under the paths parameter in a JSON file (like myconfig.json).

                    ...
    {
        "paths": {
            "math": "/SuiteScripts/Example/math.js"
        }
        "shim":{
            "math": {
                "exports": "myMath"
            }
        }
    }
    ... 
    
                  
  3. To load the custom module in your SuiteScript 2.x script, include the @NAmdConfig JSDoc tag with your .json file name and pass the custom module name to 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)
                });
            }
        }
    }); 
    
                  

Related Topics

General Notices