0

I have an issue with exporting a module in Node.js. Here is a part of my code:

var zmq = require('zmq');
var module = require('module');

var DeviceRequester = function(port, name)
{
   ...   
};

var SMValueGetter = function(socket)
{
   ...
};

module.exports.DeviceRequester = DeviceRequester;
module.exports.SMValueGetter = SMValueGetter;

When I use it I get the following error:

module.exports.DeviceRequester = DeviceRequester;
                           ^
TypeError: Cannot set property 'DeviceRequester' of undefined
at Object.<anonymous> (<PROJECT_PATH>/node_modules/lse/lib/lse.js:168:32)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (<PROJECT_PATH>/server.js:6:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)

I have no idea why it occurs. I've red the Node.js documentation and some tutorials about exporting modules and all of them do it that way. Am I overlooking something?

6
  • 2
    tried another name just as 'mymodule' ? i think you are overwriting the default module .. Commented Oct 24, 2013 at 10:06
  • oops, yes, that's the answer, @GeoPhoenix. Commented Oct 24, 2013 at 10:07
  • When I rename var module = = require('module'); and use mymodule.exports.DeviceRequester = DeviceRequester; mymodule.exports.SMValueGetter = SMValueGetter;,I still get the same error. When I use just exports.DeviceRequester = DeviceRequester without module/mymodule it works. Commented Oct 24, 2013 at 10:17
  • 1
    why are you using mymodule.exports.. ? the common way of exporting variables is module.exports and not yourmodule.exports.. Commented Oct 24, 2013 at 10:19
  • 1
    What you should do is var your module = require('module'), not what you assign to. Commented Oct 24, 2013 at 10:21

1 Answer 1

3

Give more attentions at errors

TypeError: Cannot set property 'DeviceRequester' of undefined

at line var module=require("module"); you are overwriting the default module variable.

simple try to change at something that makes more sense, such as

var MyModule=require(..path..)

Sign up to request clarification or add additional context in comments.

3 Comments

that's because you haven't fully understand what module variable is .. exports.VAR is a short way of exporting variable that use module.exports
@DavidBulczak, you renamed the wrong part (according to the "above").
I think that I understand the Node.js module system a little bit more :) Thank you guys!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.