Jjj
Jjj

Reputation: 155

Creating Node.JS Module

I have been creating software in NodeJS for years, but I have rarely ever looked into the module side of it, since I've never needed to do that stuff myself.

I have created a test.js file, which is my module, all it contains is this (i've tried this.host, self.host, var host ... nothing is working)

var tests = function () {
    this.host = "http://127.0.0.1/";
};

exports = tests;

In my server.js I try and use it and I can never get host to actually output

var tests = require('./test.js');

console.log(tests);
console.log(tests.host);

I always get this output, saying that tests variable has no properties ... which I set in the module.

sudo node server.js
{}
undefined

Upvotes: 0

Views: 74

Answers (4)

console.log(tests()); will work if the you add return statement inside the function.

Upvotes: 0

jfriend00
jfriend00

Reputation: 707328

Basically, you have to first decide if you want your module to just have properties that can be directly accessed or if you want it to have a constructor function that creates an object when it is called that then has properties or it could even just be a regular function that you call that returns a value. You have mixed and matched the first two schemes (pieces of each) and thus it does not work. I will show you both schemes:

Here's the scheme where your module exports a constructor function from which you can create an object (when you new it):

// test.js module
var tests = function () {
    this.host = "http://127.0.0.1/";
};

module.exports = tests;


// main module server.js
var Tests = require('./test.js');

var t = new Tests();
console.log(t.host);

And, here's the scheme where you just directly export properties:

// test.js module
module.exports = {
    host: "http://127.0.0.1/"
};


// main module server.js
var tests = require('./test.js');

console.log(tests);
console.log(tests.host);

Keep in mind that whatever you assign to module.exports is what require() will return after it loads your module. So, in your first case, you're assigning a function that is intended to be a constructor function so you have to use it as a constructor function in order for it to work properly.

In my second example, I assign an object to module.exports so you can then treat it just like an object after loading the module with require(). That means you can then just directly access its properties as you would for an object.

Upvotes: 0

Halim Qarroum
Halim Qarroum

Reputation: 14103

The host variable as you defined it in the tests function is not accessible through tests's prototype.

This means that in order to access it, you should be creating a new instance of tests, using the new operator :

var Tests = require('./tests');
var instance = new Tests();
// Now you can access `instance.host`

Also, as David said, use module.exports to export your function.

Upvotes: 3

Daniel A. White
Daniel A. White

Reputation: 190945

Don't do exports = tests. Either do exports.tests = tests or module.exports = tests.

Upvotes: 1

Related Questions