{"record":{"id":"d0f6db9d03cf161c","repo":"mochajs/mocha","slug":"err-mocha-no-files-match-pattern","errorCode":"ERR_MOCHA_NO_FILES_MATCH_PATTERN","errorMessage":"Cannot find any files matching pattern \"${filepath}\"","messagePattern":"Cannot find any files matching pattern \"(.+?)\"","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/cli/lookup-files.js","lineNumber":98,"sourceCode":"      const strExtensions = extensions\n        .map((ext) => (ext.startsWith(\".\") ? ext : `.${ext}`))\n        .join(\"|\");\n      pattern = `${filepath}+(${strExtensions})`;\n      debug(\"looking for files using glob pattern: %s\", pattern);\n    }\n    files.push(\n      ...glob\n        .sync(pattern, {\n          nodir: true,\n          windowsPathsNoEscape: true,\n        })\n        // glob@8 and earlier sorted results in en; glob@9 depends on OS sorting.\n        // This preserves the older glob behavior.\n        // https://github.com/mochajs/mocha/pull/5250/files#r1840469747\n        .sort((a, b) => a.localeCompare(b, \"en\")),\n    );\n    if (!files.length) {\n      throw createNoFilesMatchPatternError(\n        `Cannot find any files matching pattern \"${filepath}\"`,\n        filepath,\n      );\n    }\n    return files;\n  }\n\n  // Handle file\n  try {\n    stat = fs.statSync(filepath);\n    if (stat.isFile() || stat.isFIFO()) {\n      return [filepath];\n    }\n  } catch {\n    // ignore error\n    return;\n  }\n","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/cli/lookup-files.js#L80-L116","documentation":"`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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Verify the pattern matches at least one file from the mocha cwd (`ls test/*.spec.js` or expand the glob in Node)","Correct the path/glob or extensions in your .mocharc, npm script, or CLI args","Catch the error by `err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN'` in programmatic usage and report a friendly message","Run mocha from the repository root or use paths relative to the correct cwd"],"exampleFix":"// before\nmocha.run(['test/**/*.tst.js']); // typo extension\n// after\nmocha.run(['test/**/*.test.js']);","handlingStrategy":"try-catch","validationCode":"const { sync } = require('glob');\nconst matches = sync(pattern);\nif (!matches.length) {\n  throw new Error(`Refusing to run mocha: pattern \"${pattern}\" matches no files`);\n}","typeGuard":"const patternHasFiles = (pattern) => require('glob').sync(pattern).length > 0;","tryCatchPattern":"try {\n  lookupFiles(filepath, extensions, recursive);\n} catch (err) {\n  if (err.code === 'ERR_MOCHA_NO_FILES_MATCH_PATTERN') {\n    console.error(`Check pattern \"${err.pattern}\" and your cwd`);\n  } else throw err;\n}","preventionTips":["Expand globs manually (or `ls`) before handing them to mocha","Quote glob patterns so the shell does not pre-expand them inconsistently","Keep spec paths relative to a known cwd and pin it in npm scripts","Add a CI precheck that each configured glob matches at least one file"],"tags":["cli","glob","no-files","error-code"],"backgroundTag":"no-files-matched-pattern","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}