Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS: module.exports property is not a function

Tags:

I have the following in a module file:

module.exports = {     myfunc: myfunc };  var myfunc = function(callback){             callback(err,reply);     }; 

In an other file I got the reference to that module

var mymodule = require('./modules/mymodule'); mymodule.myfunc(function(err, reply){ ... }); 

When I call the mymodule.myfunc() I get an error saying "property 'myfunc' is not a function". This happens only with exported functions. The same module exports some 'string' fields and these are working just fine.

like image 451
Idan Avatar asked Jan 13 '14 13:01

Idan


1 Answers

When you assign module.exports, the myfunc function is still undefined. Try to assign it after declaring it:

var myfunc = function(callback){         callback(err,reply);     };  module.exports = {     myfunc: myfunc }; 
like image 70
Aioros Avatar answered Sep 22 '22 07:09

Aioros