microsoft/playwright · error · Error

Passing functions requires the init script to be a function

Error message

Passing functions requires the init script to be a function

What it means

Error "Passing functions requires the init script to be a function" thrown in microsoft/playwright.

Source

Thrown at packages/playwright-core/src/client/page.ts:977

      };
      const result = await func(source, ...this._initializer.args.map(parseResult));
      this._channel.resolve({ result: serializeArgument(result) }, kNoTimeout).catch(() => {});
    } catch (e) {
      this._channel.reject({ error: serializeError(e) }, kNoTimeout).catch(() => {});
    }
  }
}

function trimUrl(param: any): string | undefined {
  if (isRegExp(param))
    return `/${trimStringWithEllipsis(param.source, 50)}/${param.flags}`;
  if (isString(param))
    return `"${trimStringWithEllipsis(param, 50)}"`;
}

export async function addInitScriptWithExposedFunctions(owner: Page | BrowserContext, script: Function | string | { path?: string, content?: string }, arg: any): Promise<DisposableStub> {
  if (typeof script !== 'function')
    throw new Error('Passing functions requires the init script to be a function');
  const callbacks: { name: string, disposable: DisposableObject }[] = [];
  const source = await owner._wrapApiCall(async () => {
    return await initScriptSourceWithExposedFunctions(script, arg, async (name, callback) => {
      const disposable = await owner._exposeCallbackBinding(name, callback);
      callbacks.push({ name, disposable });
    });
  }, { internal: true });
  const initScriptDisposable = DisposableObject.from((await owner._channel.addInitScript({ source }, kNoTimeout)).disposable);
  return new DisposableStub(async () => {
    for (const { name, disposable } of callbacks) {
      owner._bindings.delete(name);
      disposable.dispose().catch(() => {});
    }
    await initScriptDisposable.dispose();
  });
}

View on GitHub (pinned to c8fc3bf8d3)

When it happens

Trigger: Thrown at packages/playwright-core/src/client/page.ts:977 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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