Reputation: 105
Here's a simplified code example for what I'm doing.
foo.js (in lib directory):
exports.foo = function foo() {
this.bar = function() {
console.log("foobar!");
};
};
main.js:
var foo = require("foo");
exports.main = function(options, callbacks) {
foo.bar();
}
cmd:
>cfx run
[...]
error: An exception occurred.
[...]
TypeError: foo.bar is not a function
So basically, I can't seem to do anything with the module I've imported. Am I doing something wrong here? I've tried formatting the foo() function in a few different ways and none of them seem to be able to do anything.
Thanks!
Upvotes: 2
Views: 1828
Reputation: 57651
The result of the require()
function is essentially the exports
variable of the module - and you didn't define exports.bar
. So either you call foo.foo.bar()
in your main.js
or you import the module slightly differently:
var {foo} = require("foo");
This is the same as:
var foo = require("foo").foo;
Also, as erikvold notes in his answer, you didn't really define exports.foo.bar
.
Upvotes: 5
Reputation: 16508
Try:
var foo = exports.foo = function foo() {
};
foo.bar = function() {
console.log("foobar!");
};
Your example wouldn't work in any context.
Upvotes: 2