title: Using NPM packages in Meteor package.js
body: <p>I'm trying to use the <code>glob</code> NPM module to simplify adding files to my Meteor package:</p>
  
  <pre><code>Package.on_test(function(api) {
    Npm.depends({
      "glob": "3.2.7"
    });
  
    var glob = Npm.require("glob");
    api.add_files(glob.sync("styles/**/*.styl"), "client");
    api.add_files(glob.sync("tests/styles/**/*.@(styl|css)"), "client");
  });
  </code></pre>
  
  <p>This won't work, because the <code>glob</code> dir won't be added to <code>.npm/package/node_modules</code> until later. If I try to run <code>meteor test-packages ./</code> I get the error:</p>
  
  <blockquote>
    <p>can't find npm module 'glob'. Did you forget to call 'Npm.depends'?</p>
  </blockquote>
  
  <p>However, I can trick Meteor into making this work by doing this:</p>
  
  <pre><code>Package.on_test(function(api) {
    Npm.depends({
      "glob": "3.2.7"
    });
  
    var glob = Npm.require("glob");
    if (glob) {
      api.add_files(glob.sync("styles/**/*.styl"), "client");
      api.add_files(glob.sync("tests/styles/**/*.@(styl|css)"), "client");
    }
  });
  </code></pre>
  
  <p>Now, the first time I run <code>meteor test-packages</code>, the app loads the NPM dependencies and then crashes. If I run it again, the <code>glob</code> package has been added and everything works correctly. So I'm wondering if there is a better way to use Npm modules other than Underscore and the built in modules like <code>fs</code> and <code>path</code> inside of <code>package.js</code>?</p>
  
  <p>I realize that I can emulate this functionality with <code>fs</code>, but the globpattern syntax is much more terse and readable.</p>