mastra-ai/mastra · error

Could not find mastracode package root from ${startDir}

Error message

Could not find mastracode package root from ${startDir}

What it means

findMastraCodePackageRoot walks upward from a start directory looking for the mastracode package root (identified by a marker such as package.json containing the package name). If it reaches the filesystem root without finding the marker, it throws this error, because plugin linking cannot proceed without knowing where the mastracode package lives.

Source

Thrown at mastracode/sdk/src/plugins/package-link.ts:56

      return currentDir;
    }
    if (packageName === '@mastra/code-sdk') {
      // The `mastracode` package sits next to the sdk: mastracode/sdk ↔
      // mastracode/tui in the source tree, node_modules/@mastra/code-sdk ↔
      // node_modules/mastracode when installed.
      for (const candidate of [
        path.resolve(currentDir, '..', 'tui'),
        path.resolve(currentDir, '..', '..', 'mastracode'),
      ]) {
        if (readPackageName(candidate) === 'mastracode') {
          return candidate;
        }
      }
    }

    const parentDir = path.dirname(currentDir);
    if (parentDir === currentDir) {
      throw new Error(`Could not find mastracode package root from ${startDir}`);
    }
    currentDir = parentDir;
  }
}

export function ensureMastraCodePackageLink(pluginDir: string): void {
  if (declaresInstallableMastraCodeDependency(pluginDir)) {
    return;
  }

  const packageRoot = mastraCodePackageRoot();
  const nodeModulesDir = path.join(pluginDir, 'node_modules');
  const linkPath = path.join(nodeModulesDir, 'mastracode');
  try {
    if (fs.realpathSync(linkPath) === fs.realpathSync(packageRoot)) {
      return;
    }
    fs.rmSync(linkPath, { recursive: true, force: true });

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Run the command from within the project/monorepo that contains the mastracode package so the upward search finds its root.
  2. Install mastracode as a dependency of the project so its package root exists under node_modules and is discoverable.
  3. If using symlinks, ensure they resolve into a directory inside the mastracode package tree.
  4. Reinstall/repair the mastracode package if its root package.json is missing (e.g. delete node_modules/mastracode and reinstall).

Example fix

// before — running from an isolated dir
$ cd /tmp/my-plugin && mastracode link
// Error: Could not find mastracode package root from /tmp/my-plugin
// after — run inside the project that depends on mastracode
$ cd ~/my-project/plugins/my-plugin && mastracode link
Defensive patterns

Strategy: validation

Validate before calling

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
try {
  const pkgPath = require.resolve('mastracode/package.json');
  console.log('mastracode root:', path.dirname(pkgPath));
} catch {
  throw new Error('mastracode is not resolvable from this project; install it first (pnpm/npm install mastracode)');
}

Try / catch

try {
  ensureMastraCodePackageLink(pluginDir);
} catch (err) {
  if (err instanceof Error && err.message.includes('Could not find mastracode package root')) {
    console.error(`Run this inside a project that has mastracode installed: ${err.message}`);
    process.exitCode = 1;
  } else throw err;
}

Prevention

When it happens

Trigger: Calling mastraCodePackageRoot/mastracodePackageRoot (via ensureMastraCodePackageLink) from a plugin directory that is not inside a workspace containing the mastracode package — e.g. a plugin installed in a global node_modules or an isolated temp dir.

Common situations: Running the plugin from outside the monorepo/project that contains mastracode; using a globally installed CLI whose plugin dir is not nested under the package; an incomplete install where mastracode's package.json is missing; symlinked plugin dirs that resolve to a path outside the package tree.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/53221dbab99ceec1. Report an issue: GitHub.