# Cannot read property 'DgraphClientStub' of undefined

**URL:** <https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563>\
**Category:** Dgraph Clients\
**Created:** [June 12, 2021, 12:05am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563 "2021-06-12T00:05:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anton](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anton/32/8196_2.png) [@Anton](https://discuss.dgraph.io/u/Anton)\
**Post date:** [June 12, 2021, 12:05am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/1 "2021-06-12T00:05:50Z")

</div>

This is probably not a bug, but more likely a lack of knowledge on my part.

I’m trying to use `Dgraph-js-http` with Sveltekit (`SvelteKit v1.0.0-next.114`).

I have the following javascript, to create a `store`:

```auto
import {writable} from "svelte/store";

export const resume_store = writable([]);

import {dgraph} from 'dgraph-js-http';
// import grpc from 'grpc';

// const dgraph = require("dgraph-js");
// const grpc = require("grpc");

const clientStub = new dgraph.DgraphClientStub(
    // addr: optional, default: "http://localhost:8080"
    "http://localhost:8080",
    // legacyApi: optional, default: false. Set to true when connecting to Dgraph v1.0.x
    false,
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
...
//create query etc

```

# Error

However, when I run `npm run dev` I get the following error:

```auto
11:31:07 am [vite] Error when evaluating SSR module /stores/resume.js:
TypeError: Cannot read property 'DgraphClientStub' of undefined
    at /stores/resume.js:11:31
    at instantiateModule (/home/anton/git/resume-web/node_modules/vite/dist/node/chunks/dep-cb562f8f.js:68783:166)
Cannot read property 'DgraphClientStub' of undefined
TypeError: Cannot read property 'DgraphClientStub' of undefined
    at /stores/resume.js:11:31
    at instantiateModule (/home/anton/git/resume-web/node_modules/vite/dist/node/chunks/dep-cb562f8f.js:68783:166)

```

### What Dgraph client (and version) are you using?

(put “x” in the box to select)

- Dgo
- PyDgraph
- Dgraph4J
- Dgraph-js
- Dgraph-js-http
- [Dgraph.NET](http://Dgraph.NET)

Version: 21.3.1

# Update

I was able to move past the above error by using the following:

```auto
import * as dgraph from 'dgraph-js-http';

```

Now the error I’m facing is:

```auto
500

Buffer2 is undefined

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

```

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [June 13, 2021, 10:05pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/2 "2021-06-13T22:05:50Z")

</div>

> [@Anton](#):
>
> Sveltekit

Is it a front + backend framework? I wouldn’t recommend to use Dgraph at frontend tho. Always put your DB behind an API. Never expose it in the front-end without a safe manner. These errors happen when the framework isn’t supported. NodeJS logic is always supported tho.

---

<div class="post-metadata">

**Author:** ![Anton](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anton/32/8196_2.png) [@Anton](https://discuss.dgraph.io/u/Anton)\
**Post date:** [June 13, 2021, 10:28pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/3 "2021-06-13T22:28:02Z")

</div>

Thanks @MichelDiz

This is a frontend framework.

> [@MichelDiz](#):
>
> Always put your DB behind an API

I understand your point however the primary reason I’m using dgraph is because I can avoid the usual layers, eg, database, dao, business logic, public api etc.

Also, in this particular case, the usage of the application is internal and trusted.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [June 13, 2021, 10:35pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/4 "2021-06-13T22:35:36Z")

</div>

So, you can use the RAW HTTP requests(Via Axios, fetch and so on). It is much more simple. In order for us to support the framework you are using, it will require time to analyze what is missing and what can be done. In other words, it will take time. JS support for Dgraph is preferably “framework-less”.

---

<div class="post-metadata">

**Author:** ![Anton](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anton/32/8196_2.png) [@Anton](https://discuss.dgraph.io/u/Anton)\
**Post date:** [June 13, 2021, 10:46pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/5 "2021-06-13T22:46:06Z")

</div>

Ok, thanks I’ll try using `fetch`.

Regarding your previous point. If you recommend “Always put your DB behind an API” why is the dgraph graphql api not sufficient to be exposed publicly? And can you recommend any simply and good graphql proxy apis?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [June 13, 2021, 11:23pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/6 "2021-06-13T23:23:40Z")

</div>

No, you can expose the GraphQL API for sure. Just add the auth feature and you are good to go. Is the DQL part that isn’t safe to expose.

---

<div class="post-metadata">

**Author:** ![Anton](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anton/32/8196_2.png) [@Anton](https://discuss.dgraph.io/u/Anton)\
**Post date:** [June 13, 2021, 11:47pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/7 "2021-06-13T23:47:18Z")

</div>

> [@MichelDiz](#):
>
> No, you can expose the GraphQL API for sure. Just add the auth feature and you are good to go.

Excellent. This is very good to know.

> [@MichelDiz](#):
>
> Is the DQL part that isn’t safe to expose.

Can you expand on this a little please.  
Is there are Dql endpoint and a separate Graphql endpoint?  
And is the dql endpoint more sensitive?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [June 13, 2021, 11:51pm UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/8 "2021-06-13T23:51:46Z")

</div>

> [@Anton](#):
>
> Is there are Dql endpoint and a separate Graphql endpoint?

Yes. There is `/graphql` and for DQL `/query` and `/mutate` and `/commit`.

> [@Anton](#):
>
> And is the dql endpoint more sensitive?

Yes, it is open. And DQL you have a much powerful query to modify the DB or mess up with things.

DQL is for pros who will handle the DB by hand.  
GraphQL is for casual and frontend users who doesn’t wanna bother with implementation in the back-end. As the GraphQL feature is practically autonomous. It generate your CRUD schema out of the blue for you.

---

<div class="post-metadata">

**Author:** ![Anton](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/anton/32/8196_2.png) [@Anton](https://discuss.dgraph.io/u/Anton)\
**Post date:** [June 14, 2021, 12:22am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/9 "2021-06-14T00:22:16Z")

</div>

> [@MichelDiz](#):
>
> DQL is for pros who will handle the DB by hand.

Oh no. Now I’m facing an identity crisis. I’m using dql but am no pro!

Thank you again @MichelDiz - your guidance is excellent!

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [June 14, 2021, 12:25am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/10 "2021-06-14T00:25:17Z")

</div>

> [@Anton](#):
>
> I’m using dql but am no pro!

lol

pros be like lollll

Kidding, that just a way of saying it. You can be a pro too, just protect your DB 😛

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [June 14, 2021, 3:54am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/11 "2021-06-14T03:54:31Z")

</div>

> [@Anton](#):
>
> Can you expand on this a little please.  
> Is there are Dql endpoint and a separate Graphql endpoint?  
> And is the dql endpoint more sensitive?

> [@New to Dgraph? DQL vs. GraphQL, Endpoints, Headers, and Schema Translation](http://discuss.hypermode.com/t/new-to-dgraph-dql-vs-graphql-endpoints-headers-and-schema-translation/10443):
>
> I would like to better understand the relationship between the DQL schemas and the GraphQL schemas This statement and a multitude of related questions (see References) have prompted me to write this to help new users. I remember when I started on the Dgraph road. Why were there so many endpoints? Which one should I use when? What is graphql±? (Meaning to me “GraphQL more or less.” [Now it is commonly called DQL to avoid confusion and easier to say/write](http://discuss.hypermode.com/t/poll-rename-graphql/9010)) Should I write my schema in GraphQL or …

---

<div class="post-metadata">

**Author:** ![samadadi](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@samadadi](https://discuss.dgraph.io/u/samadadi)\
**Post date:** [July 27, 2021, 1:12am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/12 "2021-07-27T01:12:57Z")

</div>

I am using vitejs as well and this is the error I am getting:  
dgraph-js-http: ^21.3.1

Note: I tried this [react to-do app](https://dgraph.io/blog/post/building-todo-list-react-dgraph/) example using vitejs.

 ![Screenshot from 2021-07-27 05-38-06](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/1/1278c030188f710c6cb584b8c2cbd8f996e63333.png)

 ![Screenshot from 2021-07-27 05-37-58](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2a5a44c377471eac1f5e312cf58f3295f2967b26.png)

I suppose in this case vitejs can’t resolve commonjs modules correctly. So there is unknown Buffer in this case. I try too find why is this happening and how to solve this problem. But If you have a solution please share. Thank you so much.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [July 27, 2021, 1:39am UTC](https://discuss.dgraph.io/t/cannot-read-property-dgraphclientstub-of-undefined/14563/13 "2021-07-27T01:39:29Z")

</div>

This should be a new topic.
