Find a file or directory by walking up parent directories
JavaScript
Clone or download
Latest commit 2319b79 Jun 18, 2018
Permalink
Failed to load latest commit information.
fixture Support searching multiple files (#12) Dec 2, 2016
.editorconfig Meta tweaks Feb 18, 2018
.gitattributes Meta tweaks Jun 18, 2018
.gitignore Meta tweaks Feb 18, 2018
.npmrc Meta tweaks Feb 18, 2018
.travis.yml Require Node.js 6 (#24) Jun 18, 2018
appveyor.yml Require Node.js 6 (#24) Jun 18, 2018
index.js Require Node.js 6 (#24) Jun 18, 2018
license Meta tweaks Feb 18, 2018
package.json 3.0.0 Jun 18, 2018
readme.md Meta tweaks Jun 18, 2018
test.js Minor tweaks May 4, 2017

readme.md

find-up Build Status: Linux and macOS Build Status: Windows

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

const findUp = require('find-up');

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

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

API

findUp(filename, [options])

Returns a Promise for either the filepath or null if it couldn't be found.

findUp([filenameA, filenameB], [options])

Returns a Promise for either the first filepath found (by respecting the order) or null if none could be found.

findUp.sync(filename, [options])

Returns a filepath or null.

findUp.sync([filenameA, filenameB], [options])

Returns the first filepath found (by respecting the order) or null.

filename

Type: string

Filename of the file to find.

options

Type: Object

cwd

Type: string
Default: process.cwd()

Directory to start from.

Related

  • find-up-cli - CLI for this module
  • pkg-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

License

MIT © Sindre Sorhus