denoland/deno · error · TypeError

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

Error message

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

What it means

Deno.FsFile wraps an OS file resource whose lifecycle the runtime owns. Direct construction with new Deno.FsFile(rid) is not allowed: the constructor requires an internal key (fsFileConstructorKey) that only Deno.open()/Deno.openSync() (and internal creators like the stdio singletons) pass. This guard keeps rid management under runtime control.

Solutions

  1. Use Deno.open(path, opts) / Deno.openSync(path, opts) to obtain an FsFile
  2. For standard streams use the singletons Deno.stdin, Deno.stdout, Deno.stderr
  3. Wrap or compose an FsFile returned by open() rather than constructing one; if another API returns an rid, use that API's own handle type

Example fix

// before
const f = new Deno.FsFile(3);

// after
const f = await Deno.open("./data.txt", { read: true });
Defensive patterns

Strategy: fallback

Validate before calling

if (f instanceof Deno.FsFile) {
  // already a real FsFile from open()/openSync()
} else {
  f = await Deno.open(path, { read: true });
}

Type guard

const isFsFile = (v) => v instanceof Deno.FsFile;

Prevention

When it happens

Trigger: new Deno.FsFile(3); new Deno.FsFile(Deno.stdin.rid); attempting to fake or subclass FsFile by calling the constructor; code written for older Deno versions where constructing with an rid was possible.

Common situations: Migrating legacy scripts that wrapped raw rids; test helpers trying to construct fake file objects; interop code that receives an rid from another API and wants an FsFile view.

Related errors


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

Appendix: 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 9ad36f7a2c)