Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: EventSource is not defined when API request in Node.js #6352

Closed
1 task done
gibiee opened this issue Nov 9, 2023 · 5 comments · Fixed by #8118
Closed
1 task done

ReferenceError: EventSource is not defined when API request in Node.js #6352

gibiee opened this issue Nov 9, 2023 · 5 comments · Fixed by #8118
Assignees
Labels
bug Something isn't working gradio_client Related to the one of the gradio client libraries

Comments

@gibiee
Copy link
Contributor

gibiee commented Nov 9, 2023

Describe the bug

I am testing API request in Node.js with @gradio/client.

But, ReferenceError: EventSource is not defined is occured when I execute the code by the command.

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

"type": "module" must be set in package.json

test.js

import { client } from "@gradio/client";

const app = await client("gradio/hello_world");
const result = await app.predict("/predict", [
  "John",
]);
console.log(result.data);

Then, execute node test.js in the command

Screenshot

No response

Logs

file:///C:/source/gradio_test/nodejs_client0.7.2/node_modules/@gradio/client/dist/index.js:733
            eventSource = new EventSource(url);
            ^

ReferenceError: EventSource is not defined
    at file:///C:/source/gradio_test/nodejs_client0.7.2/node_modules/@gradio/client/dist/index.js:733:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.9.0

System Info

Node.js v20.9.0
@gradio/client 0.7.2

Severity

I can work around it

@gibiee gibiee added the bug Something isn't working label Nov 9, 2023
@gibiee
Copy link
Contributor Author

gibiee commented Nov 9, 2023

I can run the code with simple tricks, but but it doesn't seem to be clean.

  1. Declare EventSource globally.
// npm i eventsource

import { client } from "@gradio/client";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
global.EventSource = require('eventsource');

const app = await client("gradio/hello_world");
const result = await app.predict("/predict", [
  "John",
]);
console.log(result.data);

Then, another error will be occured instead of the EventSource.

node:internal/errors:497
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received an instance of URL
    at new NodeError (node:internal/errors:406:5)
    at validateString (node:internal/validators:162:11)
    at Url.parse (node:url:170:3)
    at urlParse (node:url:141:13)
    at connect (C:\source\gradio_test\nodejs_client0.7.2\node_modules\eventsource\lib\eventsource.js:90:19)
    at new EventSource (C:\source\gradio_test\nodejs_client0.7.2\node_modules\eventsource\lib\eventsource.js:283:3)
    at file:///C:/source/gradio_test/nodejs_client0.7.2/node_modules/@gradio/client/dist/index.js:733:27
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Node.js v20.9.0
  1. In node_modules/@gradio/client/dist/index.js 733 Line, add .toString() to url.
// before
eventSource = new EventSource(url);

// after
eventSource = new EventSource(url.toString());

Finally, I can get a output without the error.

@abidlabs abidlabs added the gradio_client Related to the one of the gradio client libraries label Nov 9, 2023
@PhantHive
Copy link

Hello,
I have the same issue in NodeJS with the following versions:
gradio: 4.4.1
@gradio/client 0.8.1

PS: Work around works fine for me as well

I wish all of you a nice day ☀️

@gotDaijobu
Copy link

Also got the same issue with Node/Nuxt - without using a command. I had to install eventsource ("npm i eventsource") and use the work around.

I also fixed the other call to EventSource Factory, line 1071:

event_stream = EventSource_factory(url.toString());

@pngwn
Copy link
Member

pngwn commented May 4, 2024

we broke it, will release again early next week!

@hannahblair
Copy link
Collaborator

fixed by #8252

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working gradio_client Related to the one of the gradio client libraries
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants