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!
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:
- HTML template files are always loaded before everything else
- Files beginning with main. are loaded last
- Files inside any lib/ directory are loaded next
- Files with deeper paths are loaded next
- 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:
lib
in your deep paths. E.g. rename the path like modules/questions/stuff/collections.js
.export
/import
module syntax.