0

I'm trying to add some value to the global or local object, but nothin happens.

I'm tried create: global.test = {}; in main.js And to add value test[name] = value in second js

tried create var test = {}; in second.js And to add value test[name] = value in second js

But this examples doesn't helped me.

Code in my files:

main.js:

global.common = require('./second.js'); global.test = {}

second.js:

module.exports = { 
main: function (name, value) {
            test[name] = value;
        }
};

second.js invoked in another files, but the whole point is displayed in the code above.

1 Answers1

0

Reading the NodeJS docs:

In browsers, the top-level scope is the global scope. This means that within the browser var something will define a new global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside a Node.js module will be local to that module.

Seems like you are expecting the global scope of NodeJS to work the same way that JS works in browsers but that is not the case.

See this question for more information : What is the 'global' object in NodeJS

Jimmy Leahy
  • 396
  • 1
  • 11