在Node.js中遞迴獲取資料夾中的所有文件

你如何在Node.js中列出資料夾中的所有文件? 我有一個需求,需要遞迴地獲取資料夾中的所有文件。 我發現最好的方法是安裝glob庫: npm install glob 我想在content/post資料夾中查找所有的index.md文件,每個文件都在自己的目錄結構中,可能在多個子文件夾中: content/post/first/index.md content/post/second/index.md content/post/another/test/index.md 以下是我實現的方式: const glob = require('glob') const rootFolder = 'content/post' glob(rootFolder + '/\*\*/index.md', (err, files) => { if (err) { console.log('Error', err) } else { console.log(files) } })