fsPromises.glob(pattern[, options])


  • pattern <string> | <string[]>
  • options <Object>
    • cwd <string> | <URL> 当前工作目录。默认: process.cwd()
    • exclude <Function> | <string[]> 用于过滤文件/目录的函数,或要排除的一组 glob 模式。如果提供的是函数,返回 true 表示排除该项,返回 false 表示包含该项。默认值: undefined。如果提供的是字符串数组,每个字符串都应是指定要排除路径的 glob 模式。注意:不支持取反模式(例如 '!foo.js')。
    • withFileTypes <boolean> true 如果全局匹配应该返回路径作为 Dirents,false 否则。默认值: false
  • 返回:<AsyncIterator> 一个异步迭代器,生成符合模式的文件路径。
import { glob } from 'node:fs/promises';

for await (const entry of glob('**/*.js'))
  console.log(entry);const { glob } = require('node:fs/promises');

(async () => {
  for await (const entry of glob('**/*.js'))
    console.log(entry);
})();