{"record":{"id":"a27520d5a4543df5","repo":"microsoft/playwright","slug":"cannot-evaluate-a-string-with-arguments","errorCode":null,"errorMessage":"Cannot evaluate a string with arguments","messagePattern":"Cannot evaluate a string with arguments","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/client/clientHelper.ts","lineNumber":40,"sourceCode":"import { createGuid } from '@utils/crypto';\n\nexport function envObjectToArray(env: NodeJS.ProcessEnv): { name: string, value: string }[] {\n  const result: { name: string, value: string }[] = [];\n  for (const name in env) {\n    if (!Object.is(env[name], undefined))\n      result.push({ name, value: String(env[name]) });\n  }\n  return result;\n}\n\nexport async function evaluationScript(fun: Function | string | { path?: string, content?: string }, arg?: any, addSourceUrl: boolean = true): Promise<string> {\n  if (typeof fun === 'function') {\n    const source = fun.toString();\n    const argString = Object.is(arg, undefined) ? 'undefined' : JSON.stringify(arg);\n    return `(${source})(${argString})`;\n  }\n  if (arg !== undefined)\n    throw new Error('Cannot evaluate a string with arguments');\n  if (isString(fun))\n    return fun;\n  if (fun.content !== undefined)\n    return fun.content;\n  if (fun.path !== undefined) {\n    let source = await fs.promises.readFile(fun.path, 'utf8');\n    if (addSourceUrl)\n      source = addSourceUrlToScript(source, fun.path);\n    return source;\n  }\n  throw new Error('Either path or content property must be present');\n}\n\nexport async function initScriptSourceWithExposedFunctions(fun: Function, arg: any, expose: (name: string, callback: Function) => Promise<void>): Promise<string> {\n  const exposePromises: Promise<void>[] = [];\n  const serialized = serializeAsCallArgument(arg, value => {\n    if (typeof value === 'function') {\n      const name = kFunctionBindingPrefix + createGuid();","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/client/clientHelper.ts#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a function so arg is inlined: `evaluationScript(() => myFunc(x), { x: 1 })`.","Inline the data into the string yourself if you must use a string.","Omit arg when fun is a string."],"exampleFix":"// before\nawait evaluationScript('doStuff()', { value: 42 });\n// after\nawait evaluationScript((arg) => doStuff(arg), { value: 42 });","handlingStrategy":"validation","validationCode":"if (typeof script === 'string' && arg !== undefined)\n  throw new Error('Pass a function (not a string) when supplying arguments to evaluationScript.');","typeGuard":"function isFunctionScript(v: unknown): v is Function {\n  return typeof v === 'function';\n}","tryCatchPattern":null,"preventionTips":["Prefer function-form scripts so arguments are serialized safely.","If you must use a string script, inline any needed data yourself."],"tags":["evaluation","script","validation"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}