title: Browserify Require All Files in Directory
body: <p>I'm new to Browserify (and Javascript build systems in general) and have reached a point where I am thoroughly confused.</p>
  
  <p>I have Gulp setup to do my builds, which has always worked fine, and lately I've been using Browserify to bundle -- mostly so I can separate my code into separate files and <code>require()</code> them where they need to be.</p>
  
  <p>My issue is, I have a folder with a bunch of small modules I need to require within another module, and I am trying to avoid hard coding the names of all of them. Is there a way to do a require of all of the files?</p>
  
  <p>I've tried using <a href="https://www.npmjs.com/package/bulkify" rel="noreferrer">Bulkify</a> and <a href="https://www.npmjs.com/package/folderify" rel="noreferrer">Folderify</a> but cannot get them to work. For example, with Bulkify, the package installed is called <strong>bulkify</strong> and is in the node_modules folder, but then they tell you to require <strong>bulk-require</strong>, which is in a sub node_modules folder of the bulkify package. When I try that, Browserify chokes with a <code>Cannot find module 'bulk-require'...</code> type error.</p>
  
  <p>At this point I am confused because I have no idea why the installation directions of both of those do not work (nor whether they will even help me). Am I supposed to be using them in my Gulp file? Or can I use them in one of my modules and it will get called during the Browserify?</p>
  
  <p>Here is a snippet of my build task for this if this helps:</p>
  
  <pre><code>// Report Builder
  gulp.task('script-builder', function() {
  
      // Unminified
      // **********************************************************
      browserify({entries: './resources/assets/js/report/builder.js'})
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .bundle()
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(source('builder.js'))
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(buffer())
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(gulp.dest('./public/js/debug'));
  
      // Minified
      // **********************************************************
      browserify({entries: './resources/assets/js/report/builder.js'})
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .bundle()
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(source('builder.js'))
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(buffer())
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(ext_replace('.min.js'))
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(uglify())
          .on('error', function (err) { console.log(err); this.emit('end'); })
          .pipe(gulp.dest('./public/js/dist'));
  
  });
  </code></pre>
  
  <p>I have no idea what I am doing here. Am I just going to have to hardcode the paths in my <code>require()</code> calls or is there a better way?</p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>I can clearly see <code>bulk-require</code> in the <code>bulkify</code> node module:</p>
  
  <p><img src="https://i.stack.imgur.com/TMTZm.png" alt="enter image description here"></p>
  
  <p>But, when I try to require <code>bulk-require</code>, it chokes:</p>
  
  <pre><code>module.exports = function(type, driver, node) {
  
      var propertiesContainer = '#property-container';
  
      var bulk = require('bulk-require');
      var mods = bulk(__dirname, ['properties/**/*.js']);
  
  }
  </code></pre>
  
  <blockquote>
    <p>Error: Cannot find module 'bulk-require' from '/path/to/my/project/resources/assets/js/report'</p>
  </blockquote>
  
  <hr>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I was able to make this work using the <code>require-globify</code> package (<a href="https://github.com/capaj/require-globify" rel="noreferrer">https://github.com/capaj/require-globify</a>). In my javascript, I used:</p>
  
  <pre><code>var properties = require('./properties/*.js', {mode: 'hash', resolve: ['path-reduce', 'strip-ext']});
  </code></pre>
  
  <p>That returned an object with keys as the filename without extension or the path prefix.</p>
  
  <p>In my gulpfile.js, I did this:</p>
  
  <pre><code>browserify({
      entries: './resources/assets/js/report/builder.js',
      transform: ['require-globify']
  })
  .on('error', function (err) { console.log(err); this.emit('end'); })
  .bundle()
  .on('error', function (err) { console.log(err); this.emit('end'); })
  .pipe(source('builder.js'))
  .on('error', function (err) { console.log(err); this.emit('end'); })
  .pipe(buffer())
  .on('error', function (err) { console.log(err); this.emit('end'); })
  .pipe(gulp.dest('./public/js/debug'));
  </code></pre>