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.
- 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
},
}
...
- Copy
- To load the custom module by name, specify the module file name (alias) and its path by configuring the
pathsparameter in a JSON file (for example, myconfig.json).
...
{
"paths": {
"math": "/SuiteScripts/Example/math.js"
}
"shim":{
"math": {
"exports": "myMath"
}
}
}
...
- Copy
- Load the custom module in your SuiteScript 2.x script by including the
@NAmdConfigJSDoc 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)
});
}
}
});