Why can't I call my custom test() method?
Attempt 1
app/code/Company/Test/view/frontend/web/js/test.js
define([
 'jquery',
 'domReady!'
], function($) {
 function test()
 {
 alert("it works!");
 }
});
app/code/Company/Test/view/frontend/templates/forms/default.phtml
<script>
 require(['Company_Test/js/test'], function() {
 test();
 });
</script>
Output:
Uncaught ReferenceError: test is not defined
Attempt 2
app/code/Company/Test/view/frontend/web/js/test.js
define([
 'jquery',
 'domReady!'
], function($) {
 function test()
 {
 alert("it works!");
 }
});
app/code/Company/Test/view/frontend/templates/forms/default.phtml
<script>
 require(['Company_Test/js/test'], function(script) {
 script.test();
 });
</script>
Output:
Uncaught TypeError: Cannot read properties of undefined (reading 'test')
Attempt 3
app/code/Company/Test/view/frontend/web/js/test.js
define([
 'jquery',
 'domReady!'
], function($) {
 return function test()
 {
 alert("it works!");
 }
});
app/code/Company/Test/view/frontend/templates/forms/default.phtml
<script>
 require(['Company_Test/js/test'], function(script) {
 script.test();
 });
</script>
Output:
contacts:2958 Uncaught TypeError: script.test is not a function
1 Answer 1
Figured it out:
app/code/Company/Test/view/frontend/web/js/test.js
function test()
{
 alert("it works!");
}
app/code/Company/Test/view/frontend/templates/forms/default.phtml
<script>
 require(['Company_Test/js/test'], function() {
 test();
 });
</script>