Search code examples
javascriptbrowserify

Browserify private global variable


How can I create a private custom global variable with browserify?
For example, a variable wich is accessible from all the browserify files (require()) but not outside the browserify block, console or others scripts cannot access to it.
I've tried global, but It's accessible from window / console.

EDIT: no answers? I really needs that to prevent self XSS (for eg, malicious scripts to stole user data or to send bad packets to delete his rooms ect...)

Example code:

Main.js

mycustomglobal.test  = require('blabla');
mycustomglobal.test2 = require('blablablabla');

var users = require('./users.js');

Users.js file:

console.log(mycustomglobal);
// we need to be able to get test and test2

Console / or other script

console.log(mycustomglobal)
// we need to get undefined

Solution

  • I think something like this would suit your needs. You need to create a module, which I prefer to name private_globals.js:

    var globals={};
    module.exports=globals;
    

    In your other files, you can use this module's exported object.

    var globals=require("./private_globals")
    console.log(globals.privateVar1);
    globals.privateVar2=10;
    

    I can think of no other way, unless you mess with source of browserify.