mochajs/mocha · error · Error

ERR_MOCHA_NO_FILES_MATCH_PATTERN

ERR_MOCHA_NO_FILES_MATCH_PATTERN

Error message

Cannot find any files matching pattern "${filepath}"

What it means

`lookupFiles(filepath, extensions, recursive)` is the CLI helper that resolves file arguments into concrete test files. When a pattern (including glob patterns) resolves to zero files, it throws a custom error with code ERR_MOCHA_NO_FILES_MATCH_PATTERN. It preserves glob@8-style en-locale sorting before checking, so the error fires only when nothing at all matched.

Source

Thrown at lib/cli/lookup-files.js:98

      const strExtensions = extensions
        .map((ext) => (ext.startsWith(".") ? ext : `.${ext}`))
        .join("|");
      pattern = `${filepath}+(${strExtensions})`;
      debug("looking for files using glob pattern: %s", pattern);
    }
    files.push(
      ...glob
        .sync(pattern, {
          nodir: true,
          windowsPathsNoEscape: true,
        })
        // glob@8 and earlier sorted results in en; glob@9 depends on OS sorting.
        // This preserves the older glob behavior.
        // https://github.com/mochajs/mocha/pull/5250/files#r1840469747
        .sort((a, b) => a.localeCompare(b, "en")),
    );
    if (!files.length) {
      throw createNoFilesMatchPatternError(
        `Cannot find any files matching pattern "${filepath}"`,
        filepath,
      );
    }
    return files;
  }

  // Handle file
  try {
    stat = fs.statSync(filepath);
    if (stat.isFile() || stat.isFIFO()) {
      return [filepath];
    }
  } catch {
    // ignore error
    return;
  }

View on GitHub (pinned to 6bcbee4fd9)

Solutions

  1. Verify the pattern matches at least one file from the mocha cwd (`ls test/*.spec.js` or expand the glob in Node)
  2. Correct the path/glob or extensions in your .mocharc, npm script, or CLI args
  3. Catch the error by `err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN'` in programmatic usage and report a friendly message
  4. Run mocha from the repository root or use paths relative to the correct cwd

Example fix

// before
mocha.run(['test/**/*.tst.js']); // typo extension
// after
mocha.run(['test/**/*.test.js']);
Defensive patterns

Strategy: try-catch

Validate before calling

const { sync } = require('glob');
const matches = sync(pattern);
if (!matches.length) {
  throw new Error(`Refusing to run mocha: pattern "${pattern}" matches no files`);
}

Type guard

const patternHasFiles = (pattern) => require('glob').sync(pattern).length > 0;

Try / catch

try {
  lookupFiles(filepath, extensions, recursive);
} catch (err) {
  if (err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN') {
    console.error(`Check pattern "${err.pattern}" and your cwd`);
  } else throw err;
}

Prevention

When it happens

Trigger: `mocha 'test/*.spec.js'` where no file matches the glob; a literal path that does not exist; a directory argument whose contained files match none of the given extensions; running from the wrong working directory so relative paths miss.

Common situations: Shell quotes stripped so glob expansion happens elsewhere, or patterns quoted when the shell should expand them; typos in extensions lists (e.g. passing no extensions for a directory); specs moved/renamed after a refactor; CI checked out a partial tree.

Related errors


AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01). Data as JSON: /api/errors/d0f6db9d03cf161c. Report an issue: GitHub.