jestjs/jest · error · Error

Can't find a root directory while resolving a config file…

Error message

Can't find a root directory while resolving a config file path.
Provided path to resolve: ${pathToResolve}
cwd: ${cwd}

What it means

Thrown by resolveConfigPath when the resolved absolute path is neither an existing file nor an existing directory (fs.existsSync returns false). This guard prevents a non-existent path from silently falling through to directory traversal, which would unexpectedly resolve to a parent package.json and run unrelated tests.

Solutions

  1. Verify the path exists: ls <path>
  2. Check for typos and correct the relative-vs-absolute resolution
  3. If the intent is directory traversal, pass the parent directory that actually exists
  4. Ensure CI checks out the workspace containing the path

Example fix

// before
jest --config ./jest.config.tss

// after
jest --config ./jest.config.ts
Defensive patterns

Strategy: validation

Validate before calling

import * as fs from 'node:fs';
import * as path from 'node:path';
function assertPathExists(p: string, cwd: string): void {
  const abs = path.isAbsolute(p) ? p : path.resolve(cwd, p);
  if (!fs.existsSync(abs)) {
    throw new Error(`Path does not exist: ${abs}`);
  }
}

Type guard

function pathExists(p: string): boolean {
  return fs.existsSync(p);
}

Prevention

When it happens

Trigger: Passing a path that does not exist on disk: a typo in --config, a deleted project directory, or a relative path resolved against the wrong cwd.

Common situations: Typos in CLI --config flags; renamed/moved directories; CI checkouts that skipped a workspace; passing a glob pattern where a path is expected.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/d4c40b3a9e3429c8. Report an issue: GitHub.

Appendix: source

Thrown at packages/jest-config/src/resolveConfigPath.ts:52

    ? pathToResolve
    : path.resolve(cwd, pathToResolve);

  if (isFile(absolutePath)) {
    return absolutePath;
  }

  // This is a guard against passing non existing path as a project/config,
  // that will otherwise result in a very confusing situation.
  // e.g.
  // With a directory structure like this:
  //   my_project/
  //     package.json
  //
  // Passing a `my_project/some_directory_that_doesnt_exist` as a project
  // name will resolve into a (possibly empty) `my_project/package.json` and
  // try to run all tests it finds under `my_project` directory.
  if (!fs.existsSync(absolutePath)) {
    throw new Error(
      "Can't find a root directory while resolving a config file path.\n" +
        `Provided path to resolve: ${pathToResolve}\n` +
        `cwd: ${cwd}`,
    );
  }

  return resolveConfigPathByTraversing(
    absolutePath,
    pathToResolve,
    cwd,
    skipMultipleConfigError,
  );
}

const resolveConfigPathByTraversing = (
  pathToResolve: string,
  initialPath: string,
  cwd: string,

View on GitHub (pinned to 8e6d128e4a)