Workspace Root Detector

3.3.1 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates both asynchronous and synchronous methods of finding the workspace root, including specifying a custom current working directory.

import { workspaceRoot, workspaceRootSync } from 'workspace-root';

// Asynchronous usage
async function findAsyncRoot() {
  const path = await workspaceRoot();
  if (path) {
    console.log('The workspace root (async) is: ', path);
  } else {
    console.log('No workspace root found (async).');
  }
}

findAsyncRoot();

// Synchronous usage
const syncPath = workspaceRootSync();
if (syncPath) {
  console.log('The workspace root (sync) is: ', syncPath);
} else {
  console.log('No workspace root found (sync).');
}

// Example with custom current working directory
// For demonstration, let's assume '/tmp' exists and you want to search from there.
// In a real scenario, you'd pass a path relevant to your project structure.
const customCwd = process.env.TEMP || '/tmp'; // Use a temporary directory for example
console.log(`Searching from custom CWD: ${customCwd}`);

workspaceRoot(customCwd).then(path => {
  console.log(`The workspace root from ${customCwd} (async) is: `, path);
});

console.log(`The workspace root from ${customCwd} (sync) is: `, workspaceRootSync(customCwd));

view raw JSON →