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. Node also has built-in fs.glob() / fs.promises.glob now; see Node.js built-ins that replaced npm packages.

Install it first (glob 13 at the time of writing):

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. If you only need one directory level, see listing files in a folder.

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

glob 9 replaced the callback API with promises, and glob 10 dropped the default export. Here’s how I do it with glob 13:

import { glob } from 'glob'

const root_folder = 'content/post'

const files = await glob(root_folder + '/**/index.md')
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 a sync call, use globSync:

import { globSync } from 'glob'

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

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

import { glob } from 'glob'

const files = await glob('**/*.js', { ignore: '**/node_modules/**' })
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.

The old callback API (glob(pattern, callback)) and the default CommonJS export are gone. Use the named glob / globSync imports above.

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

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about node: