Uncaught TypeError: Cannot read property 'from' of undefined

I am creating client side app using vitejs bundler and this is the error I am getting:
dgraph-js-http: ^21.3.1

Note: I tried this react to-do app example using vitejs.

I suppose in this case vitejs can’t resolve commonjs modules correctly. So there is unknown Buffer in this case.

This is the problem, although does no work in browser space.

Could you solve this? I have the same problem right now. I am using vite and yarn

Vite and those npm packages are problematic. They can work, but there are several workarounds to deal with. It is better to use axios instead, unless you really need gRPC

e.g.

  async executeRequest(endpoint: string, data: any, options: any) {
    const { clusterUrl, slashApiKey, authToken, aclToken } = use.getState();
    try {
      const config = {
        headers: {
          "X-Auth-Token": slashApiKey,
          "X-Dgraph-AuthToken": authToken,
          "Content-Type": "application/dql",
          "X-Dgraph-AccessToken": this.aclToken,
        },
        params: options,
      };
     // endpoint can be query, mutation
      const response = await axios.post(this.currentUrl + "/" + endpoint, data, config);

      return response.data;
    } catch (error) {
      console.error(`Error executing ${endpoint} on Dgraph:`, error);
    }
  }

IMPORTANT

An update on this. All Dgraph clients are designed to run on the server. That means Dgraph.JS should be running on Node.js and not in a front-end application. Why is that? It’s simple because it is not safe to provide direct access to the database through the front-end. In theory, you should only do this in very specific cases that don’t involve end users, as it poses a serious security risk.

You should create an API or use GraphQL to handle the front-end. For those who are not using GraphQL, it is highly recommended to create your own API that sits between the backend and frontend. Avoid exposing Dgraph to the front-end at all costs.

Cheers.

You could use Next.JS and use dgraph client on just theapi and SSR pages/ components/ functions only.