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!
The result of the
require()function is essentially theexportsvariable of the module – and you didn’t defineexports.bar. So either you callfoo.foo.bar()in yourmain.jsor you import the module slightly differently:This is the same as:
Also, as erikvold notes in his answer, you didn’t really define
exports.foo.bar.