denoland/deno · error · NodeTypeError

ERR_INVALID_ARG_VALUE

ERR_INVALID_ARG_VALUE

Error message

The argument 'encoding' is invalid encoding. Received ${actual}

What it means

assertEncoding rejects any encoding string that Buffer.isEncoding does not recognize before the value reaches native code. Only the canonical names are supported (utf8/utf-8, ascii, latin1/binary, ucs2/ucs-2, utf16le/utf-16le, base64, base64url, hex); anything else — including plausible look-alikes like 'unicode' — throws ERR_INVALID_ARG_VALUE naming the offending value.

Source

Thrown at ext/node/polyfills/internal/fs/utils.mjs:168

const kIoMaxLength = 2 ** 31 - 1;

// Use 64kb in case the file type is not a regular file and thus do not know the
// actual file size. Increasing the value further results in more frequent over
// allocation for small files and consumes CPU time and memory that should be
// used else wise.
// Use up to 512kb per read otherwise to partition reading big files to prevent
// blocking other threads in case the available threads are all in use.
const kReadFileUnknownBufferLength = 64 * 1024;
const kReadFileBufferLength = 512 * 1024;

const kWriteFileMaxChunkSize = 512 * 1024;

export const kMaxUserId = 2 ** 32 - 1;

export function assertEncoding(encoding) {
  if (encoding && !Buffer.isEncoding(encoding)) {
    const reason = "is invalid encoding";
    throw new ERR_INVALID_ARG_VALUE("encoding", encoding, reason);
  }
}

export class Dirent {
  constructor(name, type, path) {
    this.name = name;
    this.parentPath = path;
    this[kType] = type;
  }

  isDirectory() {
    return this[kType] === UV_DIRENT_DIR;
  }

  isFile() {
    return this[kType] === UV_DIRENT_FILE;
  }

View on GitHub (pinned to 9ad36f7a2c)

Solutions

  1. Use a canonical Buffer encoding, most commonly 'utf8'.
  2. Validate up front: if (!Buffer.isEncoding(enc)) throw ... or fall back to 'utf8'.
  3. Normalize external values (trim, lowercase) before passing, or use TextDecoder when full WHATWG labels are needed.

Example fix

// before
const data = fs.readFileSync(p, 'unicode'); // throws

// after
const data = fs.readFileSync(p, 'utf8');
// or sanitize external input
const enc = Buffer.isEncoding(rawEnc?.trim()) ? rawEnc.trim() : 'utf8';
const data = fs.readFileSync(p, enc);
Defensive patterns

Strategy: validation

Validate before calling

import { Buffer } from 'node:buffer';
if (!Buffer.isEncoding(enc)) {
  throw new TypeError(`Unsupported encoding: ${JSON.stringify(enc)}`);
}

Type guard

function isBufferEncoding(v) {
  return typeof v === 'string' && Buffer.isEncoding(v);
}

Prevention

When it happens

Trigger: fs.readFile(p, 'unicode'); fs.readdir(dir, { encoding: 'utf8 ' }) with trailing whitespace; encodings read from config files, CLI args or HTTP headers and forwarded without validation.

Common situations: Encoding values sourced from user input or YAML/JSON config; WHATWG encoder label names (e.g. 'unicode-1-1-utf-8') that Buffer does not accept; case/whitespace drift after copy-paste.

Related errors


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