{"record":{"id":"09b188f482aa1946","repo":"denoland/deno","slug":"err-invalid-arg-type-09b188","errorCode":"ERR_INVALID_ARG_TYPE","errorMessage":"The \"options.fd\" property must be of type number or an instance of FileHandle. Received ${actual}","messagePattern":"The \"options\\.fd\" property must be of type number or an instance of FileHandle\\. Received (.+?)","errorType":"exception","errorClass":"NodeTypeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/internal/fs/streams.mjs","lineNumber":191,"sourceCode":"    stream[kFs] = options.fs || lazyFs();\n    return options.fd;\n  } else if (\n    typeof options.fd === \"object\" &&\n    ObjectPrototypeIsPrototypeOf(FileHandle.prototype, options.fd)\n  ) {\n    // When fd is a FileHandle we can listen for 'close' events\n    if (options.fs) {\n      // FileHandle is not supported with custom fs operations\n      throw new ERR_METHOD_NOT_IMPLEMENTED(\"FileHandle with fs\");\n    }\n    stream[kHandle] = options.fd;\n    stream[kFs] = FileHandleOperations(stream[kHandle]);\n    stream[kHandle][kRef]();\n    options.fd.on(\"close\", FunctionPrototypeBind(stream.close, stream));\n    return options.fd.fd;\n  }\n\n  throw new ERR_INVALID_ARG_TYPE(\n    \"options.fd\",\n    [\"number\", \"FileHandle\"],\n    options.fd,\n  );\n}\n\nexport function ReadStream(path, options) {\n  if (!(ObjectPrototypeIsPrototypeOf(ReadStream.prototype, this))) {\n    return new ReadStream(path, options);\n  }\n\n  // A little bit bigger buffer and water marks by default\n  options = copyObject(getOptions(options, kEmptyObject));\n  if (options.highWaterMark === undefined) {\n    options.highWaterMark = 64 * 1024;\n  }\n\n  if (options.autoDestroy === undefined) {","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/node/polyfills/internal/fs/streams.mjs#L173-L209","documentation":"Reached at the end of fd validation in the stream constructors: options.fd was neither a number nor an object whose prototype chain includes FileHandle.prototype. Streams accept only an integer fd or a real FileHandle, so strings, BigInts, plain objects and Promises are rejected with ERR_INVALID_ARG_TYPE listing the two accepted types.","triggerScenarios":"fs.createReadStream(p, { fd: fsPromises.open(p, 'r') }) — passing the un-awaited Promise; { fd: '3' }; a FileHandle produced by a second loaded copy of node:fs/promises so ObjectPrototypeIsPrototypeOf(FileHandle.prototype, fd) is false.","commonSituations":"Mixing promise and callback/stream APIs and forgetting await; monorepos or bundlers that load two instances of the fs polyfill so prototype checks fail; passing a wrapped or duck-typed handle object from another library.","solutions":["Await the open call first: const handle = await fsPromises.open(p, 'r'), then pass the handle.","Pass the integer descriptor: handle.fd, or the number returned by fs.openSync.","Deduplicate node:fs/promises imports (single copy in node_modules / one bundle) so prototype checks match."],"exampleFix":"// before\nconst s = fs.createReadStream(p, { fd: fsPromises.open(p, 'r') }); // Promise, throws\n\n// after\nconst handle = await fsPromises.open(p, 'r');\nconst s = fs.createReadStream(p, { fd: handle });\n// or numeric fd\nconst s = fs.createReadStream(p, { fd: handle.fd });","handlingStrategy":"type-guard","validationCode":"const fd = opts?.fd;\nconst ok = (typeof fd === 'number' && Number.isInteger(fd) && fd >= 0) ||\n  fd instanceof (await import('node:fs/promises')).FileHandle;\nif (!ok) throw new TypeError('fd must be an awaited FileHandle or an integer descriptor');","typeGuard":"import { FileHandle } from 'node:fs/promises';\nfunction isFd(v) {\n  return (typeof v === 'number' && Number.isInteger(v) && v >= 0) ||\n         v instanceof FileHandle;\n}","tryCatchPattern":"catch (e) {\n  if (e.code === 'ERR_INVALID_ARG_TYPE' && /options\\.fd/.test(e.message)) {\n    throw new TypeError('fd must come from await fsPromises.open() or be an integer', { cause: e });\n  }\n  throw e;\n}","preventionTips":["Always await fsPromises.open before using the result as fd.","Type wrappers as fd: number | FileHandle and guard at the boundary.","Avoid loading node:fs/promises from multiple bundle paths — prototype checks break."],"tags":["filesystem","streams","filehandle","promises","invalid-arg-type"],"backgroundTag":"invalid-argument-type","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}