{"record":{"id":"5c79e45599c256e7","repo":"denoland/deno","slug":"deno-fsfile-cannot-be-constructed-use-deno-ope","errorCode":null,"errorMessage":"'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'Deno.openSync()' instead","messagePattern":"'Deno\\.FsFile' cannot be constructed, use 'Deno\\.open\\(\\)' or 'Deno\\.openSync\\(\\)' instead","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ext/fs/30_fs.js","lineNumber":652,"sourceCode":"    create: true,\n  });\n}\n\nclass FsFile {\n  #rid = 0;\n\n  #readable;\n  #writable;\n\n  constructor(rid, symbol) {\n    ObjectDefineProperty(this, internalRidSymbol, {\n      __proto__: null,\n      enumerable: false,\n      value: rid,\n    });\n    this.#rid = rid;\n    if (!symbol || symbol !== fsFileConstructorKey) {\n      throw new TypeError(\n        \"'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'Deno.openSync()' instead\",\n      );\n    }\n  }\n\n  write(p) {\n    return write(this.#rid, p);\n  }\n\n  writeSync(p) {\n    return writeSync(this.#rid, p);\n  }\n\n  truncate(len) {\n    return op_fs_file_truncate_async(this.#rid, coerceLen(len));\n  }\n\n  truncateSync(len) {","sourceCodeStart":634,"sourceCodeEnd":670,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/fs/30_fs.js#L634-L670","documentation":"Deno.FsFile's constructor is gated behind an internal symbol (fsFileConstructorKey) that only Deno.open/openSync and stdio initializers pass. Constructing a FsFile directly throws a TypeError by design, because raw resource ids (rids) are an unstable internal concept and files must originate from the owning APIs.","triggerScenarios":"new Deno.FsFile(3) or new Deno.FsFile(anyRid) — the check fires regardless of arguments because the private key symbol is missing.","commonSituations":"Upgrading from older Deno versions where new Deno.FsFile(rid) was allowed; code that manufactured files from resource ids obtained via Deno.resources(); libraries wrapping rids directly instead of the file APIs.","solutions":["Use Deno.open(path, opts) / Deno.openSync(path, opts) to obtain a FsFile","Use Deno.stdin / Deno.stdout / Deno.stderr for the standard streams","If you held a rid from another API, use that API's own handle instead of re-wrapping it in FsFile"],"exampleFix":"// before\nconst file = new Deno.FsFile(rid); // TypeError\n\n// after\nconst file = Deno.openSync(\"/tmp/data.txt\", { read: true });","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"function openLikeLegacyFsFile(ridLike) {\n  // there is no supported way to wrap a raw rid; obtain files via open/openSync\n  return Deno.openSync(path, { read: true, write: true });\n}","preventionTips":["Always create files with Deno.open / Deno.openSync","Use Deno.stdin/stdout/stderr for the standard streams","Never persist or pass raw resource ids across APIs; keep the owning handle","Treat rid-based code as legacy: refactor when upgrading Deno versions"],"tags":["fs","file","constructor","api-change","rid"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}