I have a js file that contains common variables/functions used on the client side of the application - these are in the location /client/lib/global.js
I have a file called myapp.js located at /client/myapp.js which contains a load of functions that use the global variables / functions defined in global.js. However, Meteor is throwing the error saying that it cannot see the variables / functions in global.js. Why would this be?
what does your global.js look like?
It contains a bunch of glabal javascript vars and functions.
Have you read the load order rules at meteor docs?
try to rename myapp.js
into main.myapp.js
from Meteor Docs (structure):
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
nav.html
main.html
client/lib/methods.js
client/lib/styles.js
lib/feature/styles.js
lib/collections.js
client/feature-y.js
feature-x.js
client/main.js
For example, the files above are arranged in the correct load order. main.html is loaded second because HTML templates are always loaded first, even if it begins with main., since rule 1 has priority over rule 2. However, it will be loaded after nav.html because rule 2 has priority over rule 5.
client/lib/styles.js and lib/feature/styles.js have identical load order up to rule 4; however, since client comes before lib alphabetically, it will be loaded first.
I added “console.log” statements to the top of global.js and myapp.js and I can see that global.js is loaded first. Is it something to do with the scope of the variables/functions global.js that are causing them not to be seen by those in myapp.js ?
p.s. I have tried changing the scope by adding/removing ‘var’ to the variables / functions in global.js, but it didn’t make any difference. Functions are declared using the myFunc = function(){ … }; syntax.
Hm not sure what is wrong on your side, I tested:
client/lib/global.js:
myGlobalVar = "Hello World"
myGlobalFunc = function() {
console.log(myGlobalVar);
}
client/sample.js:
console.log(myGlobalVar);
myGlobalFunc();
Shows 2 times “Hello world” on console;