Naming a Custom Module

You can set up a custom module name to aid re-use. After you configure a module name, you can require it without knowing the path.

Custom module loading by name also enables better interoperability with third-party libraries and may offer some protection against naming conflicts.

You will need to call define(id, [dependencies,] moduleObject) and configure a require Function.

To load a custom module by name:

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

  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
        }, 
    }
    ... 
    
                  
  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"
            }
        }
    }
    ... 
    
                  
  3. 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)
                });
            }
        }
    }); 
    
                  

Related Topics

SuiteScript 2.x Custom Modules
SuiteScript 2.x Anatomy of a Custom Module Script
SuiteScript 2.x Custom Module Tutorial
Module Dependency Paths
Custom Module Examples
Troubleshooting Errors
Custom Modules Frequently Asked Questions

General Notices