Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Cannot find function of undefined - CommonJS pattern

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Cannot find function of undefined - CommonJS pattern

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×