ProductPromotion
Logo

Node.JS

made by https://0x3d.site

GitHub - sindresorhus/find-up: Find a file or directory by walking up parent directories
Find a file or directory by walking up parent directories - sindresorhus/find-up
Visit Site

GitHub - sindresorhus/find-up: Find a file or directory by walking up parent directories

GitHub - sindresorhus/find-up: Find a file or directory by walking up parent directories

find-up

Find a file or directory by walking up parent directories

Install

npm install find-up

Usage

/
└── Users
    └── sindresorhus
        ├── unicorn.png
        └── foo
            └── bar
                ├── baz
                └── example.js

example.js

import path from 'node:path';
import {findUp, pathExists} from 'find-up';

console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'

console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'

console.log(await findUp(async directory => {
	const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
	return hasUnicorns && directory;
}, {type: 'directory'}));
//=> '/Users/sindresorhus'

API

findUp(name, options?)

findUp(matcher, options?)

Returns a Promise for either the path or undefined if it could not be found.

findUp([...name], options?)

Returns a Promise for either the first path found (by respecting the order of the array) or undefined if none could be found.

findUpMultiple(name, options?)

findUpMultiple(matcher, options?)

Returns a Promise for either an array of paths or an empty array if none could be found.

findUpMultiple([...name], options?)

Returns a Promise for either an array of the first paths found (by respecting the order of the array) or an empty array if none could be found.

findUpSync(name, options?)

findUpSync(matcher, options?)

Returns a path or undefined if it could not be found.

findUpSync([...name], options?)

Returns the first path found (by respecting the order of the array) or undefined if none could be found.

findUpMultipleSync(name, options?)

findUpMultipleSync(matcher, options?)

Returns an array of paths or an empty array if none could be found.

findUpMultipleSync([...name], options?)

Returns an array of the first paths found (by respecting the order of the array) or an empty array if none could be found.

name

Type: string

The name of the file or directory to find.

matcher

Type: Function

A function that will be called with each directory until it returns a string with the path, which stops the search, or the root directory has been reached and nothing was found. Useful if you want to match files with certain patterns, set of permissions, or other advanced use-cases.

When using async mode, the matcher may optionally be an async or promise-returning function that returns the path.

options

Type: object

cwd

Type: URL | string
Default: process.cwd()

The directory to start from.

type

Type: string
Default: 'file'
Values: 'file' | 'directory'

The type of path to match.

allowSymlinks

Type: boolean
Default: true

Allow symbolic links to match if they point to the chosen path type.

stopAt

Type: URL | string
Default: Root directory

A directory path where the search halts if no matches are found before reaching this point.

pathExists(path)

Returns a Promise<boolean> of whether the path exists.

pathExistsSync(path)

Returns a boolean of whether the path exists.

path

Type: string

The path to a file or directory.

findUpStop

A Symbol that can be returned by a matcher function to stop the search and cause findUp to immediately return undefined. Useful as a performance optimization in case the current working directory is deeply nested in the filesystem.

import path from 'node:path';
import {findUp, findUpStop} from 'find-up';

await findUp(directory => {
	return path.basename(directory) === 'work' ? findUpStop : 'logo.png';
});

Related

  • find-up-cli - CLI for this module
  • package-up - Find the closest package.json file
  • pkg-dir - Find the root directory of an npm package
  • resolve-from - Resolve the path of a module like require.resolve() but from a given path

Articles
to learn more about the nodejs concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about NodeJS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory