jackwener/OpenCLI · error

page.evaluate string input does not accept args; use page.ev

Error message

page.evaluate string input does not accept args; use page.evaluate(fn, ...args) instead

What it means

buildEvaluateExpression accepts either a function (whose args are serialized into the call) or a pre-written string expression. Strings are treated as complete expressions and the API deliberately refuses to attach arguments to them, because there is no safe way to bind args to an arbitrary string. Passing args with a string input is therefore rejected up front.

Source

Thrown at src/browser/utils.ts:67

  // Already an IIFE: `(async () => { ... })()` or `(function() {...})()`
  if (/^\([\s\S]*\)\s*\(.*\)\s*$/.test(code)) return code;

  // Arrow function: `() => ...` or `async () => ...`
  if (/^(async\s+)?(\([^)]*\)|[A-Za-z_]\w*)\s*=>/.test(code)) return `(${code})()`;

  // Function declaration: `function ...` or `async function ...`
  if (/^(async\s+)?function[\s(]/.test(code)) return `(${code})()`;

  // Everything else: bare expression, `new Promise(...)`, etc. → evaluate directly
  return code;
}

export function buildEvaluateExpression(input: string | EvaluateFunction, args: readonly unknown[] = []): string {
  if (typeof input === 'function') {
    return serializeFunctionForEval(input, args);
  }
  if (args.length > 0) {
    throw new Error('page.evaluate string input does not accept args; use page.evaluate(fn, ...args) instead');
  }
  return wrapForEval(input);
}

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Convert the string into a real function and pass args normally: page.evaluate((x) => ..., arg).
  2. If you must keep a string, interpolate the values yourself (safely JSON.stringify them into the expression) and drop the args argument.
  3. Call buildEvaluateExpression(str) with no second argument if the string is already self-contained.

Example fix

// before
buildEvaluateExpression('(x) => x + 1', [41]);
// after
buildEvaluateExpression((x) => x + 1, [41]);
Defensive patterns

Strategy: validation

Validate before calling

function evaluateSafe(input, ...args) {
  if (typeof input === 'string' && args.length > 0) {
    throw new TypeError('string evaluate input cannot take args; pass a function instead');
  }
  return buildEvaluateExpression(input, args);
}

Type guard

const isEvaluateFunction = (input: string | EvaluateFunction): input is EvaluateFunction => typeof input === 'function';

Try / catch

try {
  return buildEvaluateExpression(input, args);
} catch (err) {
  if (err instanceof Error && err.message.includes('does not accept args')) {
    return buildEvaluateExpression(new Function('return (' + input + ')')(), args);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling buildEvaluateExpression('() => ...', [arg]) or page.evaluate('document.title', someArg) — any string input with args.length > 0.

Common situations: Refactoring code from a driver whose evaluate accepted (string, ...args); dynamically building expressions as strings while still passing parameters; copying an old evaluate('fn string', arg) call pattern.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/840d76f6f7fb313a. Report an issue: GitHub.