denoland/deno · error · Error

'truncate' option requires 'write' to be true

Error message

'truncate' option requires 'write' to be true

What it means

Truncating a file requires write access. Deno validates open-option coherence up front and throws when truncate is true while write is false or absent, because an emptying operation through a read-only handle is meaningless.

Solutions

  1. Add write: true alongside truncate: true
  2. If you only need to empty the file, call Deno.truncate(path, 0) separately with a read/write open or on its own
  3. Check dependent flag pairs inside a shared options builder

Example fix

// before
const f = await Deno.open(path, { read: true, truncate: true });

// after
const f = await Deno.open(path, { read: true, write: true, truncate: true });
Defensive patterns

Strategy: validation

Validate before calling

function normalizeOpenOptions(o) {
  if (o.truncate) o = { ...o, write: true };
  return o;
}

const f = await Deno.open(path, normalizeOpenOptions(opts));

Prevention

When it happens

Trigger: Deno.open(path, { read: true, truncate: true }); copying a read-only options object and adding truncate: true; defaults where write is false and only truncate was set.

Common situations: Log-rotation or rewrite logic that wants 'empty the file then write' but forgets write: true; developers assuming truncate is standalone like Deno.truncate(path).

Related errors


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

Appendix: source

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

  [SymbolDispose]() {
    core.tryClose(this.#rid);
  }
}

function checkOpenOptions(options) {
  if (
    ArrayPrototypeFilter(
      ObjectValues(options),
      (val) => val === true,
    ).length === 0
  ) {
    throw new Error(
      "'options' requires at least one option to be true",
    );
  }

  if (options.truncate && !options.write) {
    throw new Error(
      "'truncate' option requires 'write' to be true",
    );
  }

  const createOrCreateNewWithoutWriteOrAppend =
    (options.create || options.createNew) &&
    !(options.write || options.append);

  if (createOrCreateNewWithoutWriteOrAppend) {
    throw new Error(
      "'create' or 'createNew' options require 'write' or 'append' to be true",
    );
  }
}

function readFileSync(path) {
  return op_fs_read_file_sync(pathFromURL(path));
}

View on GitHub (pinned to 9ad36f7a2c)