@dozerg/find-up

中文文档
1.0.9发布于 2025/08/01MITTypeScriptCommonJS近 30 天下载 1.7万

通过上行父目录查找文件或目录 .

中文使用文档
文档目录3 节
  1. findUp
  2. findUp.gen
  3. findUp.sync

查找起来

npm Downloads Build Status

通过上行父目录查找文件或目录 .

目录

安装

npm i @dozerg/find-up

APIs 密码

findUp

找出文件并返回承诺 .

  • findUp(name: string, options?: Options) => Promise<string[]>
import findUp from '@dozerg/find-up';

findUp('package.json').then((results: string[]) => {
  if(results.length === 0) {
    // No files found.
  } else {
    // Found 'package.json' files in results.
    // The results are in the order they were found,
    // i.e. from the deepest to the root.
  }
})

// Find the first 'package.json' starting from specific directory.
const [package] = await findUp('package.json', { stopAtLimit: 1, cwd: 'path/to/start' });
  • findUp(names: string[], options?: Options) => Promise<string[]>
// Find the first supported eslintrc file.
const [eslintrc] = await findUp(['.eslintrc.yaml', '.eslintrc.json'], { stopAtLimit: 1 })
// If both '.eslintrc.yaml' and '.eslintrc.json' are found in a directory,
// '.eslintrc.yaml' will be returned because it's in front of the other in the array.
  • findUp(matcher: Function, options?: Options) => Promise<string[]>

matcher 类型 (directory: string) => string | undefined。在搜索中,它会和每个目录一起被调用,然后返回一个字符串或 undefined。如果返回字符串,将检查文件路径。 如果字符串是相对的,则文件路径将是 directory 并返回字符串。 如果字符串是绝对的,文件路径将是字符串.

当处于同步模式时, matcher 可能是一种协同或许诺返回的功能.

const matcher = (directory: string) => {
  if(condition_1) {
    return undefined;
    // Nothing to check
  } else if (condition_2) {
    return '/absolute/path/to/file';
    // Will check '/absolute/path/to/file'
  } else
    return 'relative/path/to/file';
    // Will check '${directory}/relative/path/to/file'
}

const results = await findUp(matcher);

findUp.gen

以平稳的方式查找文件并返回一个 Aync 生成器 .

  • findUp.gen(name: string, options?: Options) => AsyncGenerator<string>
for await (const file of findUp.gen('my.config')) {
  // Found 'my.config' file.
}
  • findUp.gen(names: string[], options?: Options) => AsyncGenerator<string>
for await (const file of findUp.gen(['my.json', 'my.yaml'])) {
  // Found either 'my.json' or 'my.yaml' file.
  // If both are found in a directory, 'my.json' will be returned
  // because it's in front of the other in the array.
}
  • findUp.gen(matcher: Function, options?: Options) => AsyncGenerator<string>
const matcher = (dir: string) => 'my.config';

for await (const file of findUp.gen(matcher)) {
  // Found 'my.config' file.
}

findUp.sync

同步查找文件 .

  • findUp.sync(name: string, options?: Options) => string[]
// Find all 'package.json' files.
const packages = findUp.sync('package.json');

// Find the first 'package.json' starting from specific directory.
const [package] = findUp.sync('package.json', { stopAtLimit: 1, cwd: 'path/to/start' });
  • findUp.sync(names: string[], options?: Options) => string[]
// Find the first supported eslintrc file.
const [eslintrc] = findUp.sync(['.eslintrc.yaml', '.eslintrc.json'], { stopAtLimit: 1 })
// If both '.eslintrc.yaml' and '.eslintrc.json' are found in a directory,
// '.eslintrc.yaml' will be returned because it's in front of the other in the array.
  • findUp.sync(matcher: Function, options?: Options) => string[]
const matcher = (dir: string) => 'my.config';

// Find all 'my.config' files.
const configs = findUp.sync(matcher);

选项

名称类型默认说明
数据string | URLprocess.cwd()要开始搜索的目录 .
类型"file" | "directory""file"可以匹配的路径类型.
停止链接numberNumber.POSITIVE_INFINITY一旦找到一些结果就停止搜索 .
站点AtPathstring | URLpath.parse(cwd).root在到达根之前停止搜索的目录路径 .
允许链接boolean真实允许符号链接在指向所选路径类型时匹配.

灵感

非常感谢 寻找 启发了这个项目.

许可证

麻省理工学院 达伊多多@gmail.com

版本与发布记录

@dozerg/find-up 共发布 10 个版本,首次发布于 2022/03/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.9latest2025/08/011.3 年前
1.0.82024/12/221.9 年前
1.0.72024/12/011.9 年前
1.0.62024/04/052 年前
1.0.52023/12/112 年前
1.0.42023/12/012 年前

关于 @dozerg/find-up 的常见问题

@dozerg/find-up 是什么?

通过上行父目录查找文件或目录 .

如何安装 @dozerg/find-up?

在项目目录运行 npm install @dozerg/find-up,或使用 pnpm add / yarn add / bun add。

@dozerg/find-up 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@dozerg/find-up 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@dozerg/find-up 可以商用吗?

@dozerg/find-up 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@dozerg/find-up 的下载量有多少?

根据 npm 官方下载统计,@dozerg/find-up 在 2026/10/13 – 2026/11/11 这 30 天内被下载 17,467 次,日均约 582 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@dozerg/find-up 还在维护吗?

最新版本 1.0.9 发布于 2025/08/01(1.3 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。