How to Access Constants in the Lib/Constants.Js File in Meteor

How can I access constants in the lib/constants.js file in Meteor?

You will need to make them global variables in order for other files to see them.

JavaScript

/lib/constants.js

THE_ANSWER = 42; // note the lack of var

/client/some-other-file.js

console.log(THE_ANSWER);

CoffeeScript

/lib/constants.coffee

@THE_ANSWER = 42

/client/some-other-file.coffee

console.log THE_ANSWER

Handling global variables in Meteor

I don't think the pattern is that bad. I would put that file in /imports though and explicitly import it.

Alternatively, you can write into Meteor.settings.public from the server, e.g., on start-up, and those values will be available on the client in the same location. You can do this without having a settings file, which is nice because it doesn't require you to make any changes between development and production.

Server:

Meteor.startup(() => {
// code to run on server at startup
Meteor.settings.public.FOO = 'bar';
});

Client:

> console.log(Meteor.settings.public.FOO);
bar

Where do I store read-only CONSTANT values I need to reference on the client and server in my Meteor application?

What's nice about Meteor is that you can store all your constants in a unique source file and have them available both on the client and the server.

This is why I'd put these constants under lib/constants.js, as you probably know the shared lib directory exported variables are available to both the client and the server.

The subtility that you seem unaware though is that even if the variables appear defined in a unique file and available on a "shared" environment, they are actually defined separately on both environments : the Node.js global context for the server and the browser window object for the client.

This is why even if a malicious user writes SOME_CRITICALLY_IMPORTANT_CONSTANT = "hacky value;" in the browser console, he is actually referencing window.SOME_CRITICALLY_IMPORTANT_CONSTANT and there is no way this is going to modify the server variable which has the same name but lives on a separate, private and secure environment.

That's why if a user messes around with his own copy of constants that are defined in his browser environment (window object), there is no way this could possibly harm the server, it will only break his UX but well, he deserved this right.

Meteor: Global constant not getting picked up from app/lib/_constants.js

Read the "file load order" section from Structuring your application:

There are several load ordering rules. They are applied sequentially to all applicable files in the application, in the priority given below:

  1. HTML template files are always loaded before everything else
  2. Files beginning with main. are loaded last
  3. Files inside any lib/ directory are loaded next
  4. Files with deeper paths are loaded next
  5. Files are then loaded in alphabetical order of the entire path

The way this is implemented, a deeply nested lib is loaded before a less deeply nested lib, which explains your problem. Here are some options:

  1. Don't use lib in your deep paths. E.g. rename the path like modules/questions/stuff/collections.js.
  2. Move your modules into packages.
  3. Upgrade to meteor 1.3 (still pre-release as of this writing) and start using the explicit export/import module syntax.

Global variables in Meteor

When you define a variable in the classic JavaScript way :

var someVar = 'someValue';

at the root of your .js file Meteor scopes it to the file using an IIFE.

If you want to define a global variable, simply don't write the var, giving :

someVar = 'someValue';

This will define a variable in all your application by default, although you may restrict it by writing that declaration in a specific recognized folder (client or server folder for example).

However this variable won't be defined absolutely first. It will be defined when Meteor runs the actual code that defines it. Thus, it may not be the best practice because you're going to struggle with load order, and it will make your code dependent on how Meteor loads files: which folder you put the file in, the name of the file... Your code is prone to messy errors if you slightly touch your architecture.

As I suggested in another closely related post you should go for a package directly!

How to create a class in a js file in a meteor-based website?

If you want something global in Meteor, don't prefix with var.

So, this should work!

Apple = function (type) {
this.type = type;
this.color = "red";
this.getInfo = getAppleInfo;
}


Related Topics



Leave a reply



Submit