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

"require is not defined" error when using with Vite #1361

Open
wutipong opened this issue Sep 12, 2023 · 6 comments
Open

"require is not defined" error when using with Vite #1361

wutipong opened this issue Sep 12, 2023 · 6 comments

Comments

@wutipong
Copy link

I'm trying to add grpc-web client into a Sveltekit-based application. Here's the page load function (in Typescript, if that matters)

export const load: PageLoad = async ({ fetch, url }) => {
    const client = new MetaServiceClient("http://localhost:9000")
    const r = await client.list(new MetaListRequest(), null)

    console.log(r)
    return {
        ok: true
    }
};

And the output is

4:46:58 AM [vite] Error when evaluating SSR module /src/lib/grpc/service_pb.js:
|- ReferenceError: require is not defined
    at eval (/home/noom/mangaweb3-frontend/src/lib/grpc/service_pb.js:15:12)
    at instantiateModule (file:///home/noom/mangaweb3-frontend/node_modules/vite/dist/node/chunks/dep-df561101.js:55974:15)

where service_pb.js is the generated js file.

@adlion
Copy link

adlion commented Oct 11, 2023

+1

2 similar comments
@ap0xF
Copy link

ap0xF commented Nov 6, 2023

+1

@rayshoo
Copy link

rayshoo commented Mar 11, 2024

+1

@sampajano
Copy link
Collaborator

@adlion @ap0xF @rayshoo Thanks for the interests!

I think this comment explained why require doesn't work directly, and provided an alternative way to make it work, and a demo app (https://github.com/a2not/vite-grpc-web).

Could you take a look at the solution and see if it works for you?

We will also look into improving this workflow to make it easier for Vite users as we go through the Typescript-ification of our codebase.

Thanks! 😃

@rayshoo
Copy link

rayshoo commented Mar 12, 2024

@sampajano Thank you for your comment. I had already confirmed this through many searches, but unfortunately, it didn't work for me as I created and used the proto stub file with javascript rather than typescript.(vite is also used as javscript) However, I did a simple build test in the repository you shared. It definitely works well. I need to study TypeScript, which I've been putting off. Thank you!!

@sampajano
Copy link
Collaborator

@rayshoo Ahh i see! Glad to hear that the demo app works at least! Typescript seems like the right bet indeed.. We are interested in improving our TS support going forward as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants