{"record":{"id":"7baa7cf9a50c464c","repo":"denoland/deno","slug":"err-invalid-arg-value-7baa7c","errorCode":"ERR_INVALID_ARG_VALUE","errorMessage":"The argument 'encoding' is invalid encoding. Received ${actual}","messagePattern":"The argument 'encoding' is invalid encoding\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/fs/utils.mjs","lineNumber":168,"sourceCode":"const kIoMaxLength = 2 ** 31 - 1;\n\n// Use 64kb in case the file type is not a regular file and thus do not know the\n// actual file size. Increasing the value further results in more frequent over\n// allocation for small files and consumes CPU time and memory that should be\n// used else wise.\n// Use up to 512kb per read otherwise to partition reading big files to prevent\n// blocking other threads in case the available threads are all in use.\nconst kReadFileUnknownBufferLength = 64 * 1024;\nconst kReadFileBufferLength = 512 * 1024;\n\nconst kWriteFileMaxChunkSize = 512 * 1024;\n\nexport const kMaxUserId = 2 ** 32 - 1;\n\nexport function assertEncoding(encoding) {\n  if (encoding && !Buffer.isEncoding(encoding)) {\n    const reason = \"is invalid encoding\";\n    throw new ERR_INVALID_ARG_VALUE(\"encoding\", encoding, reason);\n  }\n}\n\nexport class Dirent {\n  constructor(name, type, path) {\n    this.name = name;\n    this.parentPath = path;\n    this[kType] = type;\n  }\n\n  isDirectory() {\n    return this[kType] === UV_DIRENT_DIR;\n  }\n\n  isFile() {\n    return this[kType] === UV_DIRENT_FILE;\n  }\n","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/fs/utils.mjs#L150-L186","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use a canonical Buffer encoding, most commonly 'utf8'.","Validate up front: if (!Buffer.isEncoding(enc)) throw ... or fall back to 'utf8'.","Normalize external values (trim, lowercase) before passing, or use TextDecoder when full WHATWG labels are needed."],"exampleFix":"// before\nconst data = fs.readFileSync(p, 'unicode'); // throws\n\n// after\nconst data = fs.readFileSync(p, 'utf8');\n// or sanitize external input\nconst enc = Buffer.isEncoding(rawEnc?.trim()) ? rawEnc.trim() : 'utf8';\nconst data = fs.readFileSync(p, enc);","handlingStrategy":"validation","validationCode":"import { Buffer } from 'node:buffer';\nif (!Buffer.isEncoding(enc)) {\n  throw new TypeError(`Unsupported encoding: ${JSON.stringify(enc)}`);\n}","typeGuard":"function isBufferEncoding(v) {\n  return typeof v === 'string' && Buffer.isEncoding(v);\n}","tryCatchPattern":null,"preventionTips":["Treat encodings as a closed set; never forward raw user or config strings.","Fall back to 'utf8' when a value fails Buffer.isEncoding rather than letting fs throw.","Remember base64url and base64 are distinct encodings."],"tags":["filesystem","encoding","buffer","validation"],"backgroundTag":"invalid-encoding","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}