denoland/deno · error · Error

must be >= && <= . Value was

Error message

${name} must be >= ${min} && <= ${max}. Value was ${value}

What it means

Thrown by the shared Node-compat validation helper validateIntegerRange in Deno's node polyfills (ext/node/polyfills/_utils.ts:105) when an integer option falls outside the allowed [min, max] bounds. The defaults (-2147483648..2147483647) mirror Node.js int32 validation, and the message echoes the exact bounds and offending value. It exists so Deno's node: polyfills reject out-of-range integer arguments with the same error text Node.js produces.

Solutions

  1. Read the message: it names the option, the exact min/max bounds, and the value that violated them — fix the source of that value (parse, clamp, or validate user input before the call).
  2. Clamp the value to the documented range before calling the API: value = Math.max(min, Math.min(max, value)).
  3. If the value legitimately exceeds int32 (e.g. a 5GB length), restructure the operation — split into multiple reads/writes or use an API variant that accepts larger values.
  4. Add a unit test asserting your boundary values sit strictly inside the validated range so regressions surface at the source.

Example fix

// before
const len = Number(req.headers['content-length']); // e.g. 5_000_000_000
fs.readSync(fd, buf, 0, len, 0); // throws: length must be >= 0 && <= 2147483647

// after
const len = Math.min(Number(req.headers['content-length']) || 0, buf.length);
fs.readSync(fd, buf, 0, len, 0);
Defensive patterns

Strategy: validation

Validate before calling

function clampInt(value, min, max, fallback = min) {
  const n = Number(value);
  if (!Number.isInteger(n)) return fallback;
  return Math.max(min, Math.min(max, n));
}
// before calling the node: API
const length = clampInt(Number(header('content-length')), 0, buf.length);

Type guard

const isIntInRange = (v, min = -2147483648, max = 2147483647) =>
  Number.isInteger(v) && v >= min && v <= max;

Try / catch

try {
  fs.readSync(fd, buf, 0, length, 0);
} catch (e) {
  if (e instanceof RangeError || /must be >= .*&& <=/.test(e.message)) {
    length = clampInt(length, 0, buf.length); // correct and let caller decide to retry
  } else throw e;
}

Prevention

When it happens

Trigger: Passing an integer-valued option to a node: API whose polyfill validates it with validateIntegerRange (or the Node counterpart it mirrors) — e.g. fs read/write lengths and offsets larger than the buffer or int32 range, negative values where a minimum of 0 is enforced, DNS family/socket option values outside the accepted set, or sizes/counts computed from user input that overflow 32-bit bounds. Note the value must already be an integer; non-integers fail the preceding Number.isInteger check and produce the companion 'must be an integer' error instead.

Common situations: Computing a byte length or offset from untrusted input (e.g. a parsed Content-Length header) and passing it to fs/buffer APIs without clamping; passing huge retry counts, timeouts, or ports; migrating code from environments where the argument was silently truncated; passing a numeric-looking string such as '1000' which fails earlier type checks or reaches range validation as a coerced out-of-range value.

Related errors


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

Appendix: source

Thrown at ext/node/polyfills/_utils.ts:117

  for (; index + 1 < list.length; index++) {
    list[index] = list[index + 1];
  }
  ArrayPrototypePop(list);
}

function validateIntegerRange(
  value: number,
  name: string,
  min = -2147483648,
  max = 2147483647,
) {
  // The defaults for min and max correspond to the limits of 32-bit integers.
  if (!NumberIsInteger(value)) {
    throw new Error(`${name} must be 'an integer' but was ${value}`);
  }

  if (value < min || value > max) {
    throw new Error(
      `${name} must be >= ${min} && <= ${max}. Value was ${value}`,
    );
  }
}

type OptionalSpread<T> = T extends undefined ? []
  : [T];

function once<T = undefined>(
  callback: (...args: OptionalSpread<T>) => void,
) {
  let called = false;
  return function (this: unknown, ...args: OptionalSpread<T>) {
    if (called) return;
    called = true;
    FunctionPrototypeApply(callback, this, args);
  };
}

View on GitHub (pinned to 9ad36f7a2c)