Search code examples
javascriptnode.jsgulpglobal-variables

Can we share the global variables across js files without using require or module.exports?


I tried to share my global variable across js files and also in gulp task without using module.exports and require.

Here is the sample code:

File 1:

server.js

var server = new Hapi.Server()
  global.configurationManagerObj = 'Dev'

File 2:

siteconfig.js

console.log("This is global variable from server file",global.configurationManagerObj)

File 3:

gulpfile.js

gulp.task('setenv', function(done) {
    console.log("Global variable is shared in gulp task", global.configurationManagerObj);
    done();
});

But If I tried to get the variable it shows undefined.


Solution

  • your can assign variable on global process object and can access it anywhere.

    this might help read-environment-variables