denoland/deno · error · TypeError

'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'D

Error message

'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'Deno.openSync()' instead

What it means

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.

Source

Thrown at ext/fs/30_fs.js:652

    create: true,
  });
}

class FsFile {
  #rid = 0;

  #readable;
  #writable;

  constructor(rid, symbol) {
    ObjectDefineProperty(this, internalRidSymbol, {
      __proto__: null,
      enumerable: false,
      value: rid,
    });
    this.#rid = rid;
    if (!symbol || symbol !== fsFileConstructorKey) {
      throw new TypeError(
        "'Deno.FsFile' cannot be constructed, use 'Deno.open()' or 'Deno.openSync()' instead",
      );
    }
  }

  write(p) {
    return write(this.#rid, p);
  }

  writeSync(p) {
    return writeSync(this.#rid, p);
  }

  truncate(len) {
    return op_fs_file_truncate_async(this.#rid, coerceLen(len));
  }

  truncateSync(len) {

View on GitHub (pinned to 89f33cbef2)

Solutions

  1. Use Deno.open(path, opts) / Deno.openSync(path, opts) to obtain a FsFile
  2. Use Deno.stdin / Deno.stdout / Deno.stderr for the standard streams
  3. If you held a rid from another API, use that API's own handle instead of re-wrapping it in FsFile

Example fix

// before
const file = new Deno.FsFile(rid); // TypeError

// after
const file = Deno.openSync("/tmp/data.txt", { read: true });
Defensive patterns

Strategy: try-catch

Try / catch

function openLikeLegacyFsFile(ridLike) {
  // there is no supported way to wrap a raw rid; obtain files via open/openSync
  return Deno.openSync(path, { read: true, write: true });
}

Prevention

When it happens

Trigger: new Deno.FsFile(3) or new Deno.FsFile(anyRid) — the check fires regardless of arguments because the private key symbol is missing.

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

Related errors


AI-assisted analysis of denoland/deno@89f33cbef2 (2026-08-16). Data as JSON: /api/errors/5c79e45599c256e7. Report an issue: GitHub.