Import one script into another

I am writing a script that is getting so long its hard to keep in straight in the scriptable ide. I would like to break up into several files for organization purposes. I am still fairly new to coding and have never done this. I saw and old post about this topic from 2018 but hoping there is some updates. Ill show an example of what I’ve been trying unsuccessfully and hopefully there is a easy fix. For example purposes I have uses Test1.js and Test2.js both in the scriptable folder in iCloud. Test1.js would be imported into Test2

Ive messed with importModule seeing if that would work but dont seem to be getting it

Test1.js

function add(a,b){
sum = a + b
console.log(`The sum is ${sum}`)
return sum
}

Import into Test2.js


const mymath = import('Test1.js')
let a  = 5
let b = 6
mymath.add(a,b)

Receive this error

TypeError: mymath.add is not a function. (In ‘mymath.add(a,b)’, ‘mymath.add’ is undefined)

Any help would be greatly appreciated

Scriptable’s documentation should give you a good starting point with importModule.

Based on that, try this.

Test1.js

module.exports.add = function(a,b){
sum = a + b
console.log(`The sum is ${sum}`)
return sum
}

Test2.js

const mymath = importModule('Test1');
let a =5;
let b=6;
mymath.add(a, b);

As always very much appreciated. I finally get it now!!!