Cannot find function of undefined - CommonJS pattern
Problem
var revealed = function(){
var a = [1,2,3];
function abc(){
return (a[0]*a[1])+a[2]);
}
return {
name: 'revealed',
abcfn: abc
}
module.exports = revealed;
}();
How can i use CommonJS pattern with Revealing Module pattern?. When i try to do module.exports = revealed;
and try to include the module somewhere to call the function, it throws me error saying function not found
.
var module = require('module');
module.abc();
Cannot call method of undefined.
Problem courtesy of: theJava
Solution
Inside your module function, Revealed isn't defined.
What you might do is this :
var revealed = function(){
var a = [1,2,3];
function abc(){
return (a[0]*a[1])+a[2]);
}
return module.exports = {
name: 'revealed',
abcfn: abc
}
}();
Solution courtesy of: Denys Séguret
Discussion
View additional discussion.