We described it the documentation
Is missing how to write a module, You could use the `define` function
Did you try to export as AMD module?
https://requirejs.org/docs/whyamd.html
Did give it a try, seems to work with the following sample
Create module from within the JS Action
function TestExternal(message) {
// BEGIN USER CODE
addLib();
require(["test"], function (test) {
test.test(message);
});
function addLib() {
try {
require("test");
return;
}
catch (e) {
// Does not exist continue adding lib
}
define("test", [], function () {
return {
test: function(m) { console.log(m)}
}
});
}
return true;
// END USER CODE
}
From external
function TestExternal(message) {
// BEGIN USER CODE
require(["test"], function (test) {
test.test(message);
});
return true;
// END USER CODE
}
Include extern from index.html
<script src="externalModule.js?{{cachebust}}"></script>
Add the externalModule.js in the theme folder of your projec
define("test", [], function () {
return {
test: function(m) { console.log(m)}
}
});
Apparently, the module.exports does not result in the availability of function hello. The three dots under the ‘module.exports’ indicate an error. Don’t know what error because test.js does look fine to me. No other code in test.js before module.exports? Do make your IDE (Visual Studio Code?) outline your code correctly, maybe something funny shows up.
Can you replace the images by code blocks so we can copy-paste when trying to reproduce your problem?