denoland/deno · error · Error

TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value

Error message

TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "${encoding}" is invalid for option "encoding"

What it means

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.

Solutions

  1. Use a canonical encoding string: 'utf8', 'utf-8', 'latin1', 'hex', 'base64', or 'buffer'
  2. Validate before calling: if (enc) assert(Buffer.isEncoding(enc) || enc === 'buffer')
  3. Trim/normalize user-supplied encodings before passing them as options

Example fix

// before
fs.readdir(dir, 'unicode', cb); // invalid for option "encoding"

// after
fs.readdir(dir, 'utf8', cb);
// or with options object
fs.readdir(dir, { encoding: 'buffer', withFileTypes: false }, cb);
Defensive patterns

Strategy: validation

Validate before calling

if (enc && enc !== 'buffer' && !Buffer.isEncoding(enc)) throw new TypeError(`bad encoding: ${enc}`);

Type guard

const isReaddirEncoding = (v) => v == null || v === 'buffer' || Buffer.isEncoding(v);

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of denoland/deno@9ad36f7a2c (2026-08-20). Data as JSON: /api/errors/cf4e7cc01a592fc9. Report an issue: GitHub.

Appendix: source

Thrown at ext/node/polyfills/_fs/_fs_readdir.ts:135

  }
  return sortDirEntries(entries);
}

// Mirrors Node's lib/internal/fs/utils.js getOptions(): a bare string options
// arg is treated as { encoding: <string> }.
function normalizeOptions(
  options: readDirOptions | string | null | undefined,
): readDirOptions | null {
  if (typeof options === "string") {
    return { encoding: options };
  }
  return options ?? null;
}

function validateEncoding(encoding: string | undefined) {
  if (!encoding || encoding === "buffer") return;
  if (!Buffer.isEncoding(encoding)) {
    throw new Error(
      `TypeError [ERR_INVALID_OPT_VALUE_ENCODING]: The value "${encoding}" is invalid for option "encoding"`,
    );
  }
}

export function readdir(
  path: string | Buffer | URL,
  options: readDirOptions | string,
  callback: readDirCallback,
): void;
export function readdir(
  path: string | Buffer | URL,
  options: readDirOptions | string,
  callback: readDirCallbackDirent,
): void;
export function readdir(path: string | URL, callback: readDirCallback): void;
export function readdir(
  path: string | Buffer | URL,

View on GitHub (pinned to 9ad36f7a2c)