Samudra
Samudra

Reputation: 1233

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

My app directory structure is:

App
├── client
├── lib
│   ├── _constants.js
│   ├── config
│   └── router
├── modules
│   ├── answers
│   └── questions
├── node_modules
│   └── bcrypt
├── public
│   └── imgs
├── server
│   ├── lib
│   ├── roles
│   └── startup
└── settings-example.json

In my _constants.js, I have defined some global variables, e.g. Schemas = {} which I intend to use in the modules > module_name> lib > collections.js or modules > module_name> lib > methods.js

But the global variables are not found in the modules' collections.js. Here's the error I get:

W20160323-21:38:58.977(-7)? (STDERR) ReferenceError: Schemas is not defined
W20160323-21:38:58.977(-7)? (STDERR)     at modules/answers/lib/collections.js:22:1
W20160323-21:38:58.977(-7)? (STDERR)     at modules/answers/lib/collections.js:89:1

By my understanding, the global variables in the APP/lib/_constants.js file should have been loaded before the deeper modules/module_name/lib/collections.js got loaded, right?

But that's obviously not happening. What am I doing wrong?

Thanks for your help!

Upvotes: 0

Views: 248

Answers (1)

David Weldon
David Weldon

Reputation: 64342

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.

Upvotes: 1

Related Questions