简体   繁体   中英

Node module.exports returns undefined

I am having an issue with Node.js and module.exports . I understand that module.exports is a call to return an object, that object having whatever properties it is assigned.

If I have a file structure like this:

// formatting.js

function Format(text) {
    this.text = text;
}

module.exports = Format;

with this:

// index.js

var formatting = require('./formatting');

Is there a way to initialize a Format object and use it like this?

formatting('foo');
console.log(formatting.text);

Whenever I try to do it that way, I get an error that says formatting is not a function . I then have to do it like this:

var x = new formatting('foo');
console.log(x.text);

which seems cumbersome.

In modules like keypress and request , they can be used right out of the gate, like this:

var keypress = require('keypress');

keypress(std.in);

or

var request = require('request);

request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Show the HTML for the Google homepage.
  }
})

How does this work?

I'd suggest wrapping the new call in it's own function then returning that:

function Format(text) {
  this.text = text;
}

function formatting(text) {
  return new Format(text);
}

module.exports = formatting;

This way you should still be able to do:

var format = formatting('foo');
console.log(format.text);


Edit :

In terms of the request stuff, one thing you have to remember is that in JavaScript, functions are still objects. This means you can still add properties and methods to them. This is what they're doing in request though overall it's a bit too complicated to explain every detail in this answer. From what I can tell, they add a bunch of methods (functions on an object) to the request function. This is why you can immediately call those methods like request(blah, blah).pipe(blah).on(blah) Based on what's returned from calling the request function, you can chain some of the other methods on the back of it. When you're using request it's not an object, it's a function (but still technically an object). To demonstrate how functions are still objects and how it's possible to add methods to them, check out this simple example code:

function hey(){
  return;
}

hey.sayHello = function(name) {
  console.log('Hello ' + name);
} 

hey.sayHello('John'); //=> Hello John

This is basically what they're doing, just a lot more complicated and with a lot more stuff going on.

module.exports = Format;

This will return the Format constructor when you will require('./formatting')

On the other hand the code below will return an instance of Format , which you can directly call methods on:

module.exports = new Format();

Try this:

module formatting:

function Format() {
    this.setter = function(text) {
      this.text = text;
    }
    this.show = function() {
      console.log(this.text);
    } 
}
//this says I want to return empty object of Format type created by Format constructor.

module.exports = new Format();

index.js

var formatting = require('./formatting');
formatting('Welcome');
console.log(formatting.show());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM