mfc
mfc

Reputation: 3026

export a property in a nodejs module

in nodejs i can export functions like so :-

modules.exports = function() {
    return {
        func1 : function() {
            ...
            ...
        },
        func1 : function() {
            ...
            ...
        }
    }
}

is it possible to export a non function so I can use it like so?

var foo = require('bar');
var x = foo.func1();
var y = foo.property1;

Upvotes: 0

Views: 3216

Answers (2)

E.K
E.K

Reputation: 120

in your file to import put the following line :

module.export.var = "value";

and in your code

var foo = require('bar');
console.log(foo.var);

Upvotes: 1

PatrickD
PatrickD

Reputation: 1156

Yes, just add it to your returned object:

modules.exports = function() {
    return {
        func1 : function() {
            ...
            ...
        },
        func1 : function() {
            ...
            ...
        },
        property1: 'Some value'
    }
}

Upvotes: 3

Related Questions