The `workspace-root` package provides a robust utility for programmatically identifying the root directory of a monorepo workspace. It supports various popular package managers and monorepo tools including Yarn, pnpm, Lerna, and Bun. The library exposes both synchronous (`workspaceRootSync`) and asynchronous (`workspaceRoot`) functions, allowing developers to choose the appropriate API for their context, with an optional `cwd` parameter to specify the starting search path. Currently stable at version 3.3.1, the package demonstrates a consistent release cadence with frequent minor updates and patches, and underwent a significant architectural refactor in version 3.0.0. Its primary differentiator is its broad compatibility across different monorepo configurations and package manager specifics, such as Yarn's `nohoist` option, ensuring accurate root detection even in complex setups.
npm install workspace-rootVerified import paths — ran on the pinned version, not inferred.
Demonstrates both asynchronous and synchronous methods of finding the workspace root, including specifying a custom current working directory.
Review your build pipeline and module resolution configurations, especially if you encountered issues after upgrading from a pre-3.0.0 version. Ensure your bundler (e.g., Webpack, Rollup) correctly resolves ESM and CJS outputs.
Upgrade to `workspace-root@3.3.0` or higher to ensure proper compatibility with `yarn workspaces.nohoist` configurations.
Always `await workspaceRoot()` in an `async` function or use `workspaceRoot().then(path => { ... })` to correctly retrieve the workspace path.When `null` is returned unexpectedly, verify your monorepo's `package.json` `workspaces` configuration and `pnpm-workspace.yaml` (if applicable) adheres to standard conventions. Consider providing an explicit `cwd` parameter to narrow the search scope if the issue persists.
Ensure the package is installed: `npm install workspace-root`, `yarn add workspace-root`, or `pnpm add workspace-root`. Check your `node_modules` directory and `NODE_PATH` environment variable if running in a non-standard environment.
For ESM, use `import { workspaceRoot } from 'workspace-root';`. For CJS, use `const { workspaceRoot } = require('workspace-root');`. Ensure you are destructuring correctly based on the module type.To get the resolved value, `await` the function call within an `async` context: `const path = await workspaceRoot();` or use a Promise callback: `workspaceRoot().then(path => console.log(path));`.
No dependency data recorded yet.