gradio icon indicating copy to clipboard operation
gradio copied to clipboard

No "exports" main defined in `@gradio/client` - nestjs and other

Open lisandropuzzolo opened this issue 1 year ago • 18 comments

Describe the bug

I tried the gradio node client on new and existings nestjs project and all give me this error:

Error: No "exports" main defined in projects/nest-gradio/node_modules/@gradio/client/package.json
    at new NodeError (node:internal/errors:387:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:365:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:589:7)
    at resolveExports (node:internal/modules/cjs/loader:554:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:594:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1012:27)
    at Function.Module._load (node:internal/modules/cjs/loader:871:27)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (projects/nest-gradio/src/app.controller.ts:3:1)

and this is my minimal app.controller.ts:

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { client } from '@gradio/client';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  async getHello() {
    const app = await client('xlm-roberta-base');
    const info =app.view_api();

    return info;
  }
}

Is there an existing issue for this?

  • [X] I have searched the existing issues

Reproduction

  1. Create a brand new nestjs project.
$ npm i -g @nestjs/cli
$ nest new project-name
  1. Install gradio node client
npm i @gradio/client
  1. Edit the app.controller.ts to add some gradio client test:
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { client } from '@gradio/client';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  async test() {
    const app = await client('xlm-roberta-base');
    const info =app.view_api();

    return info;
  }
}

Run the app:

npm run start

The console (at least for me) then shows the mentioned error.

Screenshot

No response

Logs

Debugger listening on ws://127.0.0.1:42371/430d33b9-4fe3-4df9-8101-76e339641d68
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
Error: No "exports" main defined in /home/me/projects/nest-gradio/node_modules/@gradio/client/package.json
    at new NodeError (node:internal/errors:387:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:365:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:589:7)
    at resolveExports (node:internal/modules/cjs/loader:554:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:594:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1012:27)
    at Function.Module._load (node:internal/modules/cjs/loader:871:27)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (/home/me/projects/nest-gradio/src/app.controller.ts:3:1)
Waiting for the debugger to disconnect...
^CWaiting for the debugger to disconnect...
Waiting for the debugger to disconnect...

System Info

Gradio node client "version": "0.1.1"
OS: Ubuntu 22.04.2 LTS

[System Information]
OS Version     : Linux 5.19
NodeJS Version : v18.16.0
NPM Version    : 9.5.1 

[Nest CLI]
Nest CLI Version : 9.5.0 

[Nest Platform Information]
platform-express version : 9.4.1
schematics version       : 9.2.0
testing version          : 9.4.1
common version           : 9.4.1
core version             : 9.4.1
cli version              : 9.5.0

Severity

blocking all usage of gradio

lisandropuzzolo avatar May 18 '23 02:05 lisandropuzzolo

Thanks @lisandropuzzolo for opening the issue. cc-ing @pngwn

abidlabs avatar May 18 '23 16:05 abidlabs

Hi. Not a Next.js project, but setting "type": "module" in my package.json file did the trick for me.

Ilshidur avatar Jun 04 '23 20:06 Ilshidur

Hi @lisandropuzzolo can you see if @Ilshidur's suggestion works for you?

abidlabs avatar Jun 05 '23 15:06 abidlabs

I am also experiencing the same behaviour, node 18. Changing package.json type to module is not appropriate for my scenario.

aednzxy avatar Jun 11 '23 23:06 aednzxy

@aednzxy Can you give me more information about your usecase and perhaps an example repo?

The gradio js client is currently shipped as esm only so it does require the consuming package is a module, but we could revisit this if there is no workaround.

pngwn avatar Jun 12 '23 00:06 pngwn

I have a big legacy project that uses commonjs, it would be a nightmare to switch every file to import syntax. I cannot find any way to incorporate gradio/client. Is it possible to hit the huggingface endpoints with just axios and a bearer token ? I have tried a bit but cannot even seem to be able to find the correct endpoint url. Tried https://huggingface.co/api/spaces/${space_id} /host /predict but both say cannot be POSTed to.

galipmedia avatar Jun 20 '23 06:06 galipmedia

@galipmedia the reason we created the client is because using Gradio apps as an API can be pretty involved, so there isn't a simple way to do it.

I'll follow up the other comments in this thread shortly.

pngwn avatar Jun 20 '23 07:06 pngwn

same problem with me

kanhaiya0999 avatar Jul 05 '23 12:07 kanhaiya0999

same issue, can't use my model on HF via Gradio API :( since @gradio/client is not working, tried different Node versions on nestjs, node.js; commonjs, es6 style (require/import) nothing helps

Alex-Golovin avatar Sep 03 '23 13:09 Alex-Golovin

you may try this work-around for importing @gradio/client in commonjs module (e.g. nestjs app) this works for me:

export const importDynamic = new Function('modulePath', 'return import(modulePath)');

const { client } = await importDynamic('@gradio/client');

Sayene avatar Sep 12 '23 15:09 Sayene

same issue

ArtemKolodko avatar Sep 12 '23 20:09 ArtemKolodko

A quick and dirty workaround that works for my needs: An independent bridge ES single module that receives the arguments from the process and writes the output to stdout so it's controllable from the main app (run with child_process.spawn and read child's stdout). I had no need to pass or read files but I guess there would be no problem saving them to a common location.

jtrujillo7 avatar Sep 12 '23 23:09 jtrujillo7

same problem with me

vincent-pli avatar Sep 19 '23 07:09 vincent-pli

I have the same issue but im using ts-node? Can I use with gradio/client with ts-node?

beezzyy avatar Feb 28 '24 08:02 beezzyy

« I have a big legacy project that uses commonjs, it would be a nightmare to switch every file to import syntax. » same issue here.

arthurwolf avatar Mar 06 '24 19:03 arthurwolf

Doing

export const importDynamic = new Function('modulePath', 'return import(modulePath)');

const { client } = await importDynamic('@gradio/client');

seemingly worked, but there has to be a cleaner solution here...

(also the call doesn't seem to work/do anything, but that's probably unrelated to the import, maybe?)

arthurwolf avatar Mar 06 '24 19:03 arthurwolf

Doing the «importDynamic» solution is described above ended up not working ( I do not remember why at this point, I think I got no errors, but also no function. I guess that might have been because something unrelated to imports was broken? ).

A quick and dirty workaround that works for my needs: An independent bridge ES single module that receives the arguments from the process and writes the output to stdout so it's controllable from the main app (run with child_process.spawn and read child's stdout). I had no need to pass or read files but I guess there would be no problem saving them to a common location.

After seeing no progress I ended up implementing the same solution you have (write script, call script with execSync), except seemingly I need to pass a lot more stuff along, so the script and the caller got much more involved.

As a dev, this solution feels very nasty (barf emoji ... ).

My project uses hundreds of npm modules, and this is the only one that has this issue (I think there was one other a while back, but I had chatgpt rewrite it and then it was fine).

arthurwolf avatar Apr 11 '24 13:04 arthurwolf