fs.glob(pattern[, options], callback)


  • pattern 字符串|字符串数组
  • options <Object>
    • cwd <string> | <URL> 当前工作目录。默认值: process.cwd()
    • exclude <Function> | <string[]> 用于过滤文件/目录的函数,或者要排除的全局匹配模式列表。如果提供的是函数,返回 true 表示排除该项,返回 false 表示包含该项。默认值: undefined
    • withFileTypes <boolean> 如果为 true,则 glob 应返回路径作为 Dirent,否则为 false默认值: false
  • callback <Function>
  • 检索与指定模式匹配的文件。
import { glob } from 'node:fs';

glob('**/*.js', (err, matches) => {
  if (err) throw err;
  console.log(matches);
});const { glob } = require('node:fs');

glob('**/*.js', (err, matches) => {
  if (err) throw err;
  console.log(matches);
});