0

I'm getting a very odd error when I try to require a node module. To illustrate the problem here is the code I am trying to require:

module.exports = (function(){
  this.say = function(message) {
    console.log(message);
  }
})();

Now when I require this module I get 'Cannot read property 'say' of undefined when I try to use it as follows:

var person = require('./Person.js')
person.say('Hello World!');

And yet, if I define the module as follows it works fine ...

module.exports = {
    say : function(message) {
       console.log(message);
    }
};

I even tried this notation that also worked ...

module.exports = new Person();
function Person(){
  this.say = function(message) {
    console.log(message);
  }
};

Does anyone have any idea why the first notation doesn't work properly?

2 Answers 2

1

The reason is your first notation doesn't return anything to export.

module.exports = (function(){
  this.say = function(message) {
    console.log(message);
  }
  return this;
})();

I guess this should solve your problem.

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

4 Comments

Wouldn't the use of (function() {...})() not return an instance of an object?
Ah, no I see now what you mean. It isn't the same as invoking the new operator, but instead invokes it as a function. - Thanks!
I guess I could also use the new operator as follows ... new (function(){...}). @PrashantAgrawal thanks again!
Yeah, also see this stackoverflow.com/questions/1646698/… ....It gives great description how new works.
0

module.exports is an object that will be returned upon require calls for that file. So assume you want write a library that does some logic, some is private, other is public. and you want to expose only your public function to other people to invoke, what your file may look like is:

// Some require calls you maybe using in your lib
function privateFunc(s) {
  console.log(s);
}

function publicFunc(s) {
  privateFunc("new log: " + s);
}

module.exports = {
  log: publicFunc
}

You later will use it as follows:

var logger= require('myLib');
logger.log("my log");

This will output:

new log: my log

Thats maybe deeper than what you actually wanted, but it is meant to understand the flow of things

Comments

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.