@dozerg/find-up

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

Find files or directories by walking up parent directories.

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

Find Up

npm Downloads Build Status

Find a file or directory by walking up parent directories.

Table of contents

Install

npm i @dozerg/find-up

APIs

findUp

Find file(s) asynchrously and return a promise.

  • 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 is of type (directory: string) => string | undefined. It'll be called with every directory in the search, and return a string or undefined. If a string is returned, a file path will be checked. If the string is relative, the file path will be the join of directory and returned string. If the string is absolute, the file path will be the string.

When in async mode, matcher could be an async or promise-returning function.

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

Find file(s) asynchrously and return an async generator.

  • 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

Find file(s) synchrously.

  • 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);

Options

NameTypeDefaultDescription
cwdstring | URLprocess.cwd()The directory to start searching from.
type"file" | "directory""file"The type of paths that can match.
stopAtLimitnumberNumber.POSITIVE_INFINITYStop the search once a number of results have been found.
stopAtPathstring | URLpath.parse(cwd).rootThe path to the directory to stop the search before reaching root.
allowSymlinksbooleantrueAllow symlinks to match if they point to the chosen path type.

Inspiration

Many thanks to find-up from which this project is inspired.

License

MIT © Zhao DAI [email protected]

版本与发布记录

@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 是什么?

Find files or directories by walking up parent directories.

如何安装 @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 和提交记录判断。