grpc / grpc/grpc-web

"require is not defined" error when using with Vite

Open
#1,361 6 comments 3 reactions 0 assignees View on GitHub
typescript
Dominant language
JavaScript
Stars
9.3k
Forks
802
Avg merge
1d 7h
Merged PRs (30d)
5

Description

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

```typescript
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.

Contributor guide

Open the contributing guide

Research direction

Start with the generated src/lib/grpc/service_pb.js at line 15 and the SvelteKit page load function that imports and uses it. Reproduce the Vite SSR evaluation error, then determine the expected module behavior for this generated client. Done means the page loads without the require error and the shown client call can run.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript, vite
Domain
build-system, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.