Find the root directory of a Node.js project or npm package by walking up from a given directory.
package-directory is a Node.js library that finds the root directory of a Node.js project or npm package by searching for a `package.json` file while walking up the directory tree. It solves the problem of tools needing to reliably locate the project context from any subdirectory, which is crucial for configuration, path resolution, and build automation.
Node.js developers and tool authors who need to programmatically determine the project root for scripts, CLIs, build tools, or development utilities.
Developers choose package-directory for its simplicity, reliability, and seamless integration with the Node.js ecosystem. It provides both async and sync APIs with configurable options, making it a robust solution compared to manual directory traversal.
Find the root directory of a Node.js project or npm package
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Reliably locates the project root by searching for package.json, with an option to ignore files containing only a type field, ensuring correct handling of ESM scopes as per the README.
Provides both Promise-based async (packageDirectory) and synchronous (packageDirectorySync) functions, making it adaptable to different Node.js coding styles and use cases.
Allows setting a custom starting directory (cwd) and toggling ignoreTypeOnlyPackageJson, offering control over edge cases in modern project setups as documented in the API.
Designed to work seamlessly with related utilities like package-up and find-up, enhancing its utility in toolchains and build processes, as noted in the README's Related section.
Solely relies on package.json for detection, so it fails in projects without this file or those using alternative root markers, limiting its universality.
Stops at the first valid package.json encountered, making it unsuitable for tools that need to identify all package roots in nested monorepo structures.
The sync API can block the event loop when walking up deep directory trees, which may impact performance in I/O-heavy or real-time applications.