Flatten a folder to a JSON representation of its contents

npm install grunt-spelunk
5 downloads in the last week
16 downloads in the last month


Grunt task to traverse a folder and flatten it to a JSON representation. Previously existed as grunt-dir2json (grunt-spelunk extracts the core functionality into a separate module - spelunk - and improves performance).

See the spelunk docs for an introduction.

Getting Started

This plugin requires Grunt ~0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-spelunk --save-dev

One the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:


The "spelunk" task


In your project's Gruntfile, add a section named spelunk to the data object passed into grunt.initConfig().

  spelunk: {
    options: {
      // Task-specific options go here.
    your_target: {
      // Target-specific file lists and/or options go here.



Type: String or Array

A pattern, or array of patterns, of filenames to exclude, e.g. **/ Uses the standard globbing syntax. .DS_Store and Thumbs.db files will always be excluded - you don't need to specify these.


Type: Function or Array

Transforms values and properties when stringifying JSON. See the MDN docs


Type: String

Pretty-prints the result using this string. See the MDN docs


Type: String

If supplied, spelunk will create JSONP instead of JSON (note that the destination filename should end .js and not .json in this case)


Type: Boolean

If true, spelunk will create an AMD module (as above, extension should be .js)

Usage Examples

Default Options

This will read the contents of project/data and write a JSON file representing its contents to project/src/data.json, excluding any README files:

  spelunk: {
    data: {
      root: 'project/data',
      dest: 'project/src/data.json',
      options: {
        exclude: '**/'


In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

npm loves you