https://github.com/somewhatabstract/ancesdir
Find a specific ancestor/root directory given a starting location and a search parameter
https://github.com/somewhatabstract/ancesdir
nodejs nodejs-development nodejs-modules paths
Last synced: about 2 months ago
JSON representation
Find a specific ancestor/root directory given a starting location and a search parameter
- Host: GitHub
- URL: https://github.com/somewhatabstract/ancesdir
- Owner: somewhatabstract
- License: mit
- Created: 2019-09-17T15:46:13.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2025-04-19T14:55:58.000Z (2 months ago)
- Last Synced: 2025-04-19T18:37:54.784Z (2 months ago)
- Topics: nodejs, nodejs-development, nodejs-modules, paths
- Language: TypeScript
- Size: 6.32 MB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ancesdir
[](https://github.com/somewhatabstract/ancesdir/actions) [](https://codecov.io/gh/somewhatabstract/ancesdir) [](https://www.npmjs.com/package/ancesdir) [](https://img.shields.io/node/v/ancesdir/latest)
Find a specific ancestor/root directory given a starting location and a search parameter
There are a few packages out there that already support finding the root directory of a project based off assumptions like that directory containing `package.json` or `node_modules`. However, this is not always the case. I needed a way to find an ancestor directory that may not always have these markers. So, this provides the means to specify a custom marker file or directory as the means to identify the ancestor that you may need.
This may be useful in a variety of situations. For example, a monorepo where you want to differentiate in development scripts between the root folder of each package, and the root folder of the entire repository.
## Getting Started
### pnpm
**`pnpm add ancesdir`**### yarn
**`yarn add ancesdir`**
### npm
**`npm install ancesdir`**
## Usage
### Default
```javascript
import ancesdir from "ancesdir";console.log(ancesdir());
```Outputs the absolute path of the first parent directory to the `ancesdir` package that contains `package.json`.
In most cases, this is likely all you need.
### From Specific Location
```javascript
import ancesdir from "ancesdir";console.log(ancesdir(__dirname));
```Outputs the absolute path of the first parent directory to `__dirname` that contains `package.json`.
### Custom Target From Specific Location
```javascript
import ancesdir from "ancesdir";console.log(ancesdir(__dirname, ".mymarkerfile");
```Outputs the absolute path of the first parent directory that contains a file or directory called `.mymarkerfile`.
This is useful if you don't have a classic file hierarchy or you want to use this for more advanced use cases where having control over the file system item that identifies your ancestor is useful.