Skip to content
Recursively iterates over a directory and requires all files, returning a nested hash structure of all files
CoffeeScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
test
.gitignore
.npmignore
.travis.yml
Cakefile
LICENSE
README.md
index.coffee
index.js
package.json

README.md

Require Directory

https://nodei.co/npm/node-require-directory.png?downloads=true

Simple. Recursively iterates over a directory and require all files, returning a nested hash structure of all files. Only requires CoffeeScript and JS files.

Hat tip to require-directory. I just needed something simpler.

npm install node-require-directory

build status

assert = require 'assert'
require_directory = require '../index'
example_directory = './test/example'

describe 'require-directory', ->
    # This is the directory structure:
    #   example/
    #     - hello.coffee
    #     - hi.coffee
    #     - nested/
    #       - bleep.coffee
    #       - index.coffee
    #       - triple
    #         - yeah.coffee
    #         - index.coffee
    #
  it 'should work', ->
    dir = require_directory(example_directory)
        assert.equal dir.hello, 'world',
        assert.equal dir.hi, 'you',
        assert.equal dir.nested.bleep, 'boop'
        assert.equal dir.nested.triple.yeah, 'woo'
        assert.equal dir.nested.get, 'bloop'
        assert.equal dir.nested.triple.index, 'hello'
        assert.equal dir.nested.DS_Store, undefined
        assert.equal dir.nested.unknown_weird_file, undefined
Something went wrong with that request. Please try again.