user1288707
user1288707

Reputation:

How share a variable between two files in Node.js

Using Node.js, if I write app.js as:

  var commons = {
    title: 'myTitle',
    description: 'MyDesc',
    menu: {
      home: {
        label: 'Home',
        url: '/',
      },
      contacts: {
        label: 'Contacts',
        url: '/contacts'
      }
    }
  }

  console.log(commons);

I've this output...

  {
    title: 'myTitle',
    description: 'MyDesc',
    menu: {
      home: {
        label : 'Home',
        url: '/'
      },
      contacts: {
        label: 'Contacts',
        url: '/contacts'
      }
    }
  }

...and it works fine. But, if I would load a variable in app.js from another file (in the same path)...

commons.js:

exports.commons = {
    title: 'myTitle',
    description: 'MyDesc',
    menu: {
      home: {
        label: 'Home',
        url: '/',
      },
      contacts: {
        label: 'Contacts',
        url: '/contacts'
      }
    }
  }

app.js:

var commons = require('./commons');      
console.log(commons);

I've as output:

commons: {
        {
        title: 'myTitle',
        description: 'MyDesc',
        menu: {
            home: [Object],
            contacts: [Object]
        }
    }
 }

Why is this happening? How can I pass the variable across the two files correctly?

Upvotes: 0

Views: 1850

Answers (2)

poke
poke

Reputation: 387507

In a module, exports is an object with all the things that are exported when the module is required somewhere else. So by setting exports.commons = { ... you are basically setting the commons property of the exports object. This means that you nested your actual object in another object for the export.

In your other module, you import then the whole exports object using commons = require('./commons'). So your actual commons object that you set is at commons.commons.

If you don’t want to nest the data in another object, you can just set the exports object directly, using module.exports:

module.exports = {
    title: 'myTitle',
    description: 'MyDesc',
    ....
}

Then the import works as you expect it to.

The [Object] in the output is, as pimvdb said, just something console.log does to not go too deep in the hierarchy. The data is still there, and you will probably see the contents just fine when you remove the first level as explained above.

Upvotes: 1

Abdulsattar Mohammed
Abdulsattar Mohammed

Reputation: 10712

It's the depth to which the console.log goes. Adding one more level to the commons object gives the same response.

var commons = {
title: 'myTitle',
description: 'MyDesc',
menu: {
  home: {
    mine: {
      label: 'Home',
      url: '/',
    }
  },
  contacts: {
    label: 'Contacts',
    url: '/contacts'
  }
}
}
console.log(commons);

gives this response:

{ title: 'myTitle',
  description: 'MyDesc',
  menu:
   { home: { mine: [Object] },
     contacts: { label: 'Contacts', url: '/contacts' } } }

See this: https://groups.google.com/forum/#!topic/nodejs-dev/NmQVT3R_4cI

Upvotes: 0

Related Questions