shamon shamsudeen
shamon shamsudeen

Reputation: 5848

node.js module.exports returns with undefined

Here is my node.js

module.exports = function(){

   console.log('hello');

}

In the index.js

console.log(require('./node')());

Output

hello
undefined

Why i am getting an undefined after the function call??

Upvotes: 0

Views: 571

Answers (1)

odino
odino

Reputation: 1069

Your function doesn't return anything, it just logs to the standard output.

What really happens is that:

console.log(require('./node')()); // our original code

console.log((function(){console.log("hello")})()); // function runs, prints "hello"

console.log(); // nothing is returned by the function, so it prints "undefined"

Try changing your function to:

module.exports = function(){
   console.log('hello');

   return "HERE BE DRAGONS"
}

to see what I mean :)

Upvotes: 1

Related Questions