Skip to content

TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation #56

@fappaz

Description

@fappaz

I just created a brand new Next.js project that should integrate with "replicate": "^0.11.0", but I keep getting this error when calling the run function.

TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation

This is my function, as simple as it gets:

import Replicate from "replicate";

export async function getOutput(input) {
  const replicate = new Replicate({ auth: process.env.REPLICATE_AUTH_KEY });

  // Tried these, neither worked
  // replicate.fetch = fetch;
  // replicate.fetch = global.fetch;
  // replicate.fetch = globalThis.fetch;
  
  return await replicate.run(process.env.REPLICATE_IDENTIFIER, { input });
}

Does it mean Replicate is supposed to be used server-side only (e.g.: Node.js) and can't be called from the browser?


Edit: I managed to make it work only on Node.js, but only if I set the node-fetch (note: must be version 2 if using CommonJS instead of ES modules) as the fetch option, otherwise I get a ReferenceError: fetch is not defined.

import fetch from 'node-fetch';
import Replicate from "replicate";

export async function getOutput(input) {
  const replicate = new Replicate({
    auth: process.env.REPLICATE_AUTH_KEY,
    fetch: fetch,
  });
  
  return await replicate.run(process.env.REPLICATE_IDENTIFIER, { input });
}

Is there anything in the documentation that I might have missed that explains the right way to go about it?

Also, would appreciate it if you could have the options.fetch documented, as I only found out about it after reading the source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions