Reputation: 123
I'm trying to define a module in requirejs
and use it in another script.
I've tried a lot of things, but I cannot achieve what I want.
At the moment this is what I have.
define([],{
return {
functionOne: function(){etc, etc...}
functionTwo: function(){etc, etc...}
}
})
Then I put this in the configuration file:
requirejs.config({
paths: {myModule: pathToMyModule}
})
And then, this in the script where I want to use this module
requirejs(["myModule"], function(){
//Some code
})
But I'm still getting this errors when I try to use the defined module:
myModule is not defined.
functionOne is not defined.
functionTwo is not defined.
Am I missing something?
Thank you.
Upvotes: 2
Views: 1854
Reputation: 3117
To declare your module, you need to use a function
, so the first line in myModule.js
should look like this:
define([], function () { // ...
When you're invoking the module, you need to declare it as an argument, so your requirejs
call should look like this:
requirejs(["myModule"], function (myModule) {
// ^^^^^^^^
// Notice the argument declaration
The following worked for me:
// myModule.js
define([], function () {
return {
functionOne: function(){
console.log("Hello, world. I'm functionOne.");
},
functionTwo: function(){
console.log("Hello, world. I'm functionTwo.");
}
}
});
// require.config.js
requirejs.config({
paths: { myModule: './myModule' }
});
// index.js
requirejs(["myModule"], function (myModule) {
myModule.functionOne();
myModule.functionTwo();
})
Upvotes: 2