1. locate-path
Get the first path that exists on disk of multiple paths
locate-path
Package: locate-path
Created by: sindresorhus
Last modified: Sat, 17 Jun 2023 00:12:11 GMT
Version: 7.2.0
License: MIT
Downloads: 491,347,269
Repository: https://github.com/sindresorhus/locate-path

Install

npm install locate-path
yarn add locate-path

locate-path

Get the first path that exists on disk of multiple paths

Install

$ npm install locate-path

Usage

Here we find the first file that exists on disk, in array order.

 import {locatePath} from 'locate-path';

const files = [
	'unicorn.png',
	'rainbow.png', // Only this one actually exists on disk
	'pony.png'
];

console(await locatePath(files));
//=> 'rainbow'

API

locatePath(paths, options?)

Returns a Promise<string> for the first path that exists or undefined if none exists.

paths

Type: Iterable<string>

The paths to check.

options

Type: object

concurrency

Type: number
Default: Infinity
Minimum: 1

The number of concurrently pending promises.

preserveOrder

Type: boolean
Default: true

Preserve paths order when searching.

Disable this to improve performance if you don't care about the order.

cwd

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

The current working directory.

type

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

The type of paths that can match.

allowSymlinks

Type: boolean
Default: true

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

locatePathSync(paths, options?)

Returns the first path that exists or undefined if none exists.

paths

Type: Iterable<string>

The paths to check.

options

Type: object

cwd

Same as above.

type

Same as above.

allowSymlinks

Same as above.


Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code