{"record":{"id":"cf4e7cc01a592fc9","repo":"denoland/deno","slug":"typeerror-err-invalid-opt-value-encoding-the-va","errorCode":null,"errorMessage":"TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value \"${encoding}\" is invalid for option \"encoding\"","messagePattern":"TypeError \\[ERR_INVALID_OPT_VALUE_ENCODING\\]: The value \"(.+?)\" is invalid for option \"encoding\"","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_fs/_fs_readdir.ts","lineNumber":135,"sourceCode":"  }\n  return sortDirEntries(entries);\n}\n\n// Mirrors Node's lib/internal/fs/utils.js getOptions(): a bare string options\n// arg is treated as { encoding: <string> }.\nfunction normalizeOptions(\n  options: readDirOptions | string | null | undefined,\n): readDirOptions | null {\n  if (typeof options === \"string\") {\n    return { encoding: options };\n  }\n  return options ?? null;\n}\n\nfunction validateEncoding(encoding: string | undefined) {\n  if (!encoding || encoding === \"buffer\") return;\n  if (!Buffer.isEncoding(encoding)) {\n    throw new Error(\n      `TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value \"${encoding}\" is invalid for option \"encoding\"`,\n    );\n  }\n}\n\nexport function readdir(\n  path: string | Buffer | URL,\n  options: readDirOptions | string,\n  callback: readDirCallback,\n): void;\nexport function readdir(\n  path: string | Buffer | URL,\n  options: readDirOptions | string,\n  callback: readDirCallbackDirent,\n): void;\nexport function readdir(path: string | URL, callback: readDirCallback): void;\nexport function readdir(\n  path: string | Buffer | URL,","sourceCodeStart":117,"sourceCodeEnd":153,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/_fs/_fs_readdir.ts#L117-L153","documentation":"fs.readdir(path, options, callback) normalizes a string options argument into { encoding } and validates it: only falsy encodings and the special value 'buffer' bypass the check; every other string must satisfy Buffer.isEncoding or the polyfill throws an Error whose message embeds Node's legacy ERR_INVALID_OPT_VALUE_ENCODING text. An encoding like 'utf9' or 'hexx' therefore never reaches the directory scan.","triggerScenarios":"fs.readdir(dir, 'unicode', cb) — not a Node encoding; typos such as 'uft8'/'utf-8 ' (trailing space); passing 'ascii ' from a config file; using an encoding valid elsewhere (like 'ucs2' spelled 'ucs-2' is actually accepted — invalid ones like 'base64 ' with whitespace are not).","commonSituations":"Encoding names loaded from environment variables or CLI flags without validation; copy-pasting encoding strings from other ecosystems (Python 'utf-8' works, but Python-style 'unicode' does not); case or whitespace differences introduced by string manipulation.","solutions":["Use a canonical encoding string: 'utf8', 'utf-8', 'latin1', 'hex', 'base64', or 'buffer'","Validate before calling: if (enc) assert(Buffer.isEncoding(enc) || enc === 'buffer')","Trim/normalize user-supplied encodings before passing them as options"],"exampleFix":"// before\nfs.readdir(dir, 'unicode', cb); // invalid for option \"encoding\"\n\n// after\nfs.readdir(dir, 'utf8', cb);\n// or with options object\nfs.readdir(dir, { encoding: 'buffer', withFileTypes: false }, cb);","handlingStrategy":"validation","validationCode":"if (enc && enc !== 'buffer' && !Buffer.isEncoding(enc)) throw new TypeError(`bad encoding: ${enc}`);","typeGuard":"const isReaddirEncoding = (v) => v == null || v === 'buffer' || Buffer.isEncoding(v);","tryCatchPattern":null,"preventionTips":["Whitelist encodings in config schemas instead of accepting free strings","Trim and lowercase user-supplied encodings before use","Prefer the options-object form ({ encoding: 'utf8' }) for clarity"],"tags":["node-compat","fs","readdir","encoding","argument-validation","deno"],"backgroundTag":"invalid-encoding-option","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}