5

I'm trying to call a GRPC service (a dGraph api) from a Sveltekit (SvelteKit v1.0.0-next.114) application.

I am using a writable store from svelte/store, and am able to log to the console the response from calling the GRPC service.

However, we I import the store into my svelte component, using the following, I get the below error.

 <script>
    import {resume_store} from '../../stores/resume';
    console.log($resume_store);
 </script>

Error

500

process is not defined

node_modules/@grpc/grpc-js/build/src/logging.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:9012:28
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/@grpc/grpc-js/build/src/metadata.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:9057:21
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/@grpc/grpc-js/build/src/call-credentials.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:9231:22
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/@grpc/grpc-js/build/src/index.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:15325:30
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/util.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:15483:16
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/types.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:15595:18
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/dgraph.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:17224:15
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
node_modules/dgraph-js/lib/index.js@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:17254:18
__require@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:7:44
@http://localhost:3000/node_modules/.vite/dgraph-js.js?v=d2c49382:17261:25

Alternative Dqraph client

I also tried using dgraph-js-http client.

The observations (see below) were the same - that being, for a brief moment the page looked as expected.

Firstly, I was getting an error that looked like it was the same as described on https://github.com/vitejs/vite/issues/2579. However, I didnt save this, so cannot paste the exact error here.

I then tried

  1. deleting the node_modules
  2. running npm install
  3. running npm dev run

Then, however the following error occurred.

Error - Buffer2 is undefined


500

Buffer2 is undefined

node_modules/safe-buffer/index.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:554:9
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
node_modules/jws/lib/sign-stream.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:1126:19
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
node_modules/jws/index.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:1306:22
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
node_modules/jsonwebtoken/decode.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:1339:15
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
node_modules/jsonwebtoken/index.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:3396:15
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
node_modules/dgraph-js-http/lib/clientStub.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:3598:15
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
node_modules/dgraph-js-http/lib/index.js@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:4587:18
__require@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:7:44
@http://localhost:3000/node_modules/.vite/dgraph-js-http.js?v=e33d301e:4595:30

Other attempts included:

  • Adding export const hydrate = false; to the .svelte file, however this made no difference.
  • Related to the above point, I tried adding context="module" to the <script> tag, as this is what is shown in the Sveltekit docs related to hydrate - see https://kit.svelte.dev/docs#ssr-and-javascript-hydrate. However this resulted in an error of Cannot reference store value inside <script context="module">

Observations

For a brief moment the web page of the Sveltekit application does display the result from the dgraph grpc service call - for maybe a second. Then the error message, above, is displayed.

Magick
  • 4,603
  • 22
  • 66
  • 103
  • The `@grpc/grpc-js` library is only intended to work in Node.js. When you are getting this error, are you running it in Node.js, or somewhere else? – murgatroid99 Jun 11 '21 at 16:35
  • Well, the call to the grpc service works, and momentarily displays, before being replaced with the error message. However, your comment is interesting - while it is using node, it maybe that all js is run entirely in the browser. – Magick Jun 11 '21 at 21:12
  • I asked because `process` in the error message might be referring to the global of that name that exists in Node. If it is, the most likely cause of the error is that the code is running in some other environment. It's hard to tell though, because I can't see how you would even get a call stack in `grpc-js` with those files in that order. – murgatroid99 Jun 11 '21 at 21:22
  • 1
    My guess is the error is occurring when sveltekit attempts to hydrate. https://kit.svelte.dev/docs#ssr-and-javascript-hydrate try turning it off and see if that helps. – Chance Jun 11 '21 at 22:57
  • Thanks, I tried your recommendation, and have updated the post accordingly. – Magick Jun 12 '21 at 04:05

0 Answers0