1

I have came from writing highly object orientated code to writing javascript so i find it weird how Meteor compiles all its javascript into one big javascript file.

My issue is that I have two JavaScript files, OpenIDService.js and SteamOpenIDService.js, SteamOpenIDService.js contains a class and so does OpenIDService.js. The SteamOpenIDService class inherits from the OpenIDService class but to inherit from a different javascript file I need to include/import/require the other javascript file but I am lead to believe that Meteor does not support the 'require' function and instead it includes files in a certain order based of the folder structure which I find highly confusing.

A bit about the folder structure...

  • ./server/lib/OpenIDService.js
  • ./server/lib/SteamOpenIDService.js
  • ./server/init.js

The file init.js references the SteamOpenIDService class.

How can I make meteor include/import/require OpenIDService into the SteamOpenIDService file?

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
Francis Malloch
  • 1,074
  • 9
  • 20
  • 3
    Possible duplicate of [How can I access constants in the lib/constants.js file in Meteor?](http://stackoverflow.com/questions/26836390/how-can-i-access-constants-in-the-lib-constants-js-file-in-meteor) – Kyll Nov 13 '15 at 21:49
  • [This package](https://github.com/meteorhacks/npm) shows how to use Meteor.npmRequire() – Tim Roberts Nov 13 '15 at 21:50

1 Answers1

2

Meteor automatically imports files depth first, and in alphabetical order. Any variable that is global in your file will be exported.

If your SteamOpenIdService inherits from the OpenIDService, then their current names should work.

You'll need to make sure that OpenIDService is global (ie, no var before it).

See this answer for details on the load order.

Community
  • 1
  • 1
Ivan
  • 10,052
  • 12
  • 47
  • 78
  • they are both just normal class files(class OpenIDService {)... So i put the OpenIDService and SteamOpenIDService classes both in the same JavaScript file... Now SteamOpenIDService can reference OpenIDService but in my init.js file i cant reference SteamOpenIDService? File structure is like... - ./server/lib/openid.js(Contains both OpenIDService and SteamOpenIDService classes) - ./server/init.js(which runs this code "var openid_authenticator = new SteamOpenIDService();") I get this error, ReferenceError: SteamOpenIDService is not defined at server/init.js:8:36 – Francis Malloch Nov 13 '15 at 22:22
  • 1
    How did you define SteamOpenIDService exactly? Please post code so we can help. – Jeroen Peeters Nov 13 '15 at 23:12
  • class SteamOpenIDService extends OpenIDService { – Francis Malloch Nov 14 '15 at 23:01