Héctor Valls
Héctor Valls

Reputation: 26084

Issue exporting module NodeJS

I have this directory structure:

And the sources:

app/oauth2-home-client/oauth2-client.js

    //SOME CODE

    exports.Bearer = {

            authenticate : passport.authenticate('bearer', { session : false }),

            initialize : passport.initialize()

            // session : passport.session()
    };

app/router.js

var oauth2 = require('./oauth2-home-client/oauth2-client');

console.log(JSON.stringify(oauth2.Bearer));

//SOME CODE

When I print oauth2.Bearer (and oauth2, too) content, I get {}. What am I doing wrong?

Thanks.

Upvotes: 0

Views: 61

Answers (2)

Kan A.
Kan A.

Reputation: 61

Its value may point to the module instantiation. Have you try this?

module.exports = {...};

Upvotes: -1

t.niese
t.niese

Reputation: 40882

Your code:

exports.Bearer = {
  authenticate : passport.authenticate('bearer', { session : false }),
  initialize : passport.initialize()
  // session : passport.session()
};

Will result in:

exports.Bearer = {
  authenticate :undefined,
  initialize : undefined
};

because both passport.authenticate and passport.initialize return undefined.

And the keys having the value undefined are omitted by JSON.stringify.

[...]If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).[...]

Upvotes: 3

Related Questions