Node.js get all files in a folder recursively

By

Learn how to get all the files in a folder recursively in Node.js using the glob library and a pattern like /**/index.md to match files in any subfolder.

~~~

To get all the files in a folder recursively in Node.js, the best way I found is the glob library. You give it a pattern, and it returns every file that matches, no matter how deep in the folder tree.

Install it first:

npm install glob

The problem I had to solve

I wanted to look for all index.md files included in the content/post folder, each file being in its own directory structure, possibly under multiple subfolders:

A plain fs.readdir() call only lists the direct children of a folder, so I’d have to recurse into every subfolder myself. Glob does that for me.

How the pattern works

The magic is in the ** part of the pattern. A single * matches anything inside one folder. ** matches any number of nested folders, including none.

So content/post/**/index.md matches content/post/first/index.md and also content/post/another/test/index.md.

If you’re not sure your glob pattern matches the files you expect, you can try it in my glob pattern tester.

The code

Here’s how I did it:

const glob = require('glob')

const root_folder = 'content/post'

glob(root_folder + '/**/index.md', (err, files) => {
  if (err) {
    console.log('Error', err)
  } else {
    console.log(files)
  }
})

files is an array of paths:

[
  'content/post/first/index.md',
  'content/post/second/index.md',
  'content/post/another/test/index.md'
]

If you prefer to avoid the callback, glob.sync() returns the array directly:

const files = glob.sync(root_folder + '/**/index.md')

You can also pass an options object. The ignore option is handy to skip folders you don’t care about:

glob('**/*.js', { ignore: '**/node_modules/**' }, (err, files) => {
  console.log(files)
})

Without that, a ** pattern run at the root of a project happily walks into node_modules and returns thousands of files you never wanted.

One thing to watch out for

Glob skips files and folders starting with a dot by default. A file like content/post/.drafts/index.md won’t show up in the results. If you want those too, pass the dot: true option.

Also, always write patterns with forward slashes, even on Windows. Glob treats the backslash as an escape character, not as a path separator.

Tagged: Node.js · All topics
~~~

Related posts about node: