title: Sharing Meteor functions between server, client, and all files without declaring them global?
body: <p>Say that I've got functions that I want to re-use between multiple files on BOTH server and client. I could just make them global, place them into a <code>common</code> code folder, but that's no good.</p>
  
  <p><strong>/lib/master_file.js</strong></p>
  
  <pre><code>add = function(num1, num2){
    return num1 + num2;
  };
  
  subtract = function(num1, num2){
    return num1 - num2
  };
  </code></pre>
  
  <p><strong>/client/client_file1.js</strong></p>
  
  <pre><code>add(4,4);
  subtract(10,3);
  </code></pre>
  
  <p><strong>/server/server_file1.js</strong></p>
  
  <pre><code>add(9,1);
  </code></pre>
  
  <p><strong>/server/file2.js</strong></p>
  
  <pre><code>subtract(8,2);
  </code></pre>
  
  <p><strong>--POSSIBLE SOLUTIONS--</strong></p>
  
  <p>I can create a global object and attach the functions as values of the global object. </p>
  
  <p><strong>/lib/master_file_v2.js</strong></p>
  
  <pre><code>var add = function(num1, num2){
    return num1 + num2;
  };
  
  var subtract = function(num1, num2){
    return num1 - num2
  };
  
  global = {
  
    add: function(num1, num2){
      return add(num1, num2);
    },
    subtract: function(num1, num2){
      return subtract(num1, num2);
    }
  
  };
  </code></pre>
  
  <p>Then I would have to call the functions like so.</p>
  
  <p><strong>/client/client_file1.js</strong></p>
  
  <pre><code>var add = global.add;
  var subtract = global.subtract;
  add(4,4);
  subtract(10,3);
  </code></pre>
  
  <p><strong>/server/server_file1.js</strong></p>
  
  <pre><code>var add = global.add;
  add(9,1);
  </code></pre>
  
  <p><strong>/server/server_file2.js</strong></p>
  
  <pre><code>var subtract = global.subtract;
  subtract(8,2);
  </code></pre>
  
  <p>Is there a way to <em>not</em> call the functions like this? I would prefer to directly call them by their names but without declaring them as global.</p>
  
  <p><strong>/client/client_file1.js</strong></p>
  
  <pre><code>add(4,4);
  subtract(10,3);
  </code></pre>
  
  <p><strong>/server/server_file1.js</strong></p>
  
  <pre><code>add(9,1);
  </code></pre>
  
  <p><strong>/server/server_file2.js*</strong></p>
  
  <pre><code>subtract(8,2);
  </code></pre>
  
  <p><strong>modules</strong></p>
  
  <p>On the server side I <em>believe</em> I can use <code>module.exports</code> but modules isn't available on the client side, so that doesn't work. I could use a <code>modules</code> library for the client side but I think it might break with Meteor's unique code-sharing between the client and server in <code>/lib</code> if I were to declare <code>modules</code> there?</p>
  
  <p><strong>Meteor.methods</strong></p>
  
  <p>Calling them is pretty wordy and when defined in <code>/lib</code> they get run on both the client and server, which isn't always what you want...</p>