microsoft/playwright · error · Error

Cannot evaluate a string with arguments

Error message

Cannot evaluate a string with arguments

What it means

Thrown by evaluationScript() when `fun` is a string (not a function) and `arg` is not undefined. String scripts cannot receive serialized arguments in this helper (only function sources get arg inlined via JSON.stringify), so passing both is rejected.

Source

Thrown at packages/playwright-core/src/client/clientHelper.ts:40

import { createGuid } from '@utils/crypto';

export function envObjectToArray(env: NodeJS.ProcessEnv): { name: string, value: string }[] {
  const result: { name: string, value: string }[] = [];
  for (const name in env) {
    if (!Object.is(env[name], undefined))
      result.push({ name, value: String(env[name]) });
  }
  return result;
}

export async function evaluationScript(fun: Function | string | { path?: string, content?: string }, arg?: any, addSourceUrl: boolean = true): Promise<string> {
  if (typeof fun === 'function') {
    const source = fun.toString();
    const argString = Object.is(arg, undefined) ? 'undefined' : JSON.stringify(arg);
    return `(${source})(${argString})`;
  }
  if (arg !== undefined)
    throw new Error('Cannot evaluate a string with arguments');
  if (isString(fun))
    return fun;
  if (fun.content !== undefined)
    return fun.content;
  if (fun.path !== undefined) {
    let source = await fs.promises.readFile(fun.path, 'utf8');
    if (addSourceUrl)
      source = addSourceUrlToScript(source, fun.path);
    return source;
  }
  throw new Error('Either path or content property must be present');
}

export async function initScriptSourceWithExposedFunctions(fun: Function, arg: any, expose: (name: string, callback: Function) => Promise<void>): Promise<string> {
  const exposePromises: Promise<void>[] = [];
  const serialized = serializeAsCallArgument(arg, value => {
    if (typeof value === 'function') {
      const name = kFunctionBindingPrefix + createGuid();

View on GitHub (pinned to c8fc3bf8d3)

Solutions

  1. Pass a function so arg is inlined: `evaluationScript(() => myFunc(x), { x: 1 })`.
  2. Inline the data into the string yourself if you must use a string.
  3. Omit arg when fun is a string.

Example fix

// before
await evaluationScript('doStuff()', { value: 42 });
// after
await evaluationScript((arg) => doStuff(arg), { value: 42 });
Defensive patterns

Strategy: validation

Validate before calling

if (typeof script === 'string' && arg !== undefined)
  throw new Error('Pass a function (not a string) when supplying arguments to evaluationScript.');

Type guard

function isFunctionScript(v: unknown): v is Function {
  return typeof v === 'function';
}

Prevention

When it happens

Trigger: Calling page.evaluate or addInitScript-style helpers with a string script and an argument, e.g. `evaluationScript('myFunc()', { x: 1 })`. The typeof fun === 'function' branch is skipped; arg !== undefined triggers the throw.

Common situations: Refactoring a function-based evaluate into a string-based one and forgetting to inline the data; loading a script string and trying to parameterize it.

Related errors


AI-assisted analysis of microsoft/playwright@c8fc3bf8d3 (2026-08-12). Data as JSON: /api/errors/a27520d5a4543df5. Report an issue: GitHub.