Accessing a global app variable within a package


#1

I’m trying to access a global variable that I set in server/main.js within a meteor.method inside a package and it’s returning undefined. Here’s my code:

/* server/main.js */
myGlobalVariable = true;
/* packagedir/methods.js */
Meteor.startup(function () {
  Meteor.methods({
    myMethod: function() {
      console.log(myGlobalVariable);
    }
  });
});
/* packagedir/package.js */
Package.onUse(function (api) {
  api.versionsFrom('0.9.4');
  api.addFiles('methods.js', 'server');
  api.export('myGlobalVariable', 'server');
});

myGlobalVariable always returns undefined within the package. What am I missing?


#2

Hmmm, this says that namespacing is limited to your package and does not import names external to the package. This is a good thing. You can get global names from directly imported packages.

Every time I have considered using a global scope variable in a package, it usually means I need to move that variable definition into the package where it belongs. Then use it in the calling program rather than declaring it.