{"record":{"id":"9ea37f9f0d2ad6b0","repo":"mochajs/mocha","slug":"err-mocha-invalid-arg-type","errorCode":"ERR_MOCHA_INVALID_ARG_TYPE","errorMessage":"Argument '${extensions}' required when argument '${filepath}' is a directory","messagePattern":"Argument '(.+?)' required when argument '(.+?)' is a directory","errorType":"error_code","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/cli/lookup-files.js","lineNumber":134,"sourceCode":"\n  // Handle directory\n  fs.readdirSync(filepath).forEach((dirent) => {\n    const pathname = path.join(filepath, dirent);\n    let stat;\n\n    try {\n      stat = fs.statSync(pathname);\n      if (stat.isDirectory()) {\n        if (recursive) {\n          files.push(...lookupFiles(pathname, extensions, recursive));\n        }\n        return;\n      }\n    } catch {\n      return;\n    }\n    if (!extensions.length) {\n      throw createMissingArgumentError(\n        `Argument '${extensions}' required when argument '${filepath}' is a directory`,\n        \"extensions\",\n        \"array\",\n      );\n    }\n\n    if (\n      !stat.isFile() ||\n      !hasMatchingExtname(pathname, extensions) ||\n      isHiddenOnUnix(pathname)\n    ) {\n      return;\n    }\n    files.push(pathname);\n  });\n\n  return files;\n}","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/mochajs/mocha/blob/6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a/lib/cli/lookup-files.js#L116-L152","documentation":"When `lookupFiles` is given a directory, it needs a list of file extensions to decide which files inside the directory are test files. If the `extensions` argument is missing or an empty array while `filepath` is a directory, it throws a custom ERR_MOCHA_INVALID_ARG_TYPE error stating that extensions are required.","triggerScenarios":"`lookupFiles('./test')` with no extensions argument; `lookupFiles('./test', [])` with an empty array; programmatic callers forwarding user config where the extension list was never populated.","commonSituations":"Plugin/custom-runner authors calling the internal CLI lookup directly; config parsing that drops the `extension` array from .mocharc so an empty list reaches lookupFiles; confusion between literal file arguments (extensions optional) and directory arguments (required).","solutions":["Pass a non-empty array of extensions, e.g. `lookupFiles('./test', ['js'], true)`","Fix config loading so `extension` from .mocharc/CLI reaches the call as a populated array","If the path might be a file, pass extensions anyway or branch on `fs.statSync(filepath).isDirectory()` first","Catch by `err.code === 'ERR_MOCHA_INVALID_ARG_TYPE'` and surface which argument was invalid"],"exampleFix":"// before\nlookupFiles('./test');\n// after\nlookupFiles('./test', ['js', 'cjs', 'mjs'], true);","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nfunction safeLookupFiles(filepath, extensions, recursive) {\n  if (fs.statSync(filepath).isDirectory() && (!Array.isArray(extensions) || !extensions.length)) {\n    throw new TypeError('extensions (non-empty array) required when filepath is a directory');\n  }\n  return lookupFiles(filepath, extensions, recursive);\n}","typeGuard":"const hasExtensions = (ext) => Array.isArray(ext) && ext.length > 0;","tryCatchPattern":"try {\n  lookupFiles(filepath, extensions, recursive);\n} catch (err) {\n  if (err.code === 'ERR_MOCHA_INVALID_ARG_TYPE') {\n    console.error('Provide a non-empty extensions array for directory arguments');\n  } else throw err;\n}","preventionTips":["Always pass a non-empty extensions array when the argument may be a directory","Default extensions from mocha config (extension: ['js']) before calling","Type-check the extensions parameter in wrapper functions","Distinguish file vs directory inputs with fs.statSync before lookup"],"tags":["cli","validation","arguments","error-code"],"backgroundTag":"missing-required-argument","analyzedSha":"6bcbee4fd9a95351cedf0fdcb14c7d696486bc5a","analyzedAt":"2026-09-01T03:41:47.182Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}