Connection to a gRPC Backend Server fails
Unanswered
Ojos Azules posted this in #help-forum
Ojos AzulesOP
Hi everyone! Does anyone here have experience using Next.js with TypeScript to connect to a gRPC backend? I’m only hosting the backend server locally for now, and I keep getting DNS errors. 😦 Any advice, ideas, or solutions would be incredibly appreciated! Thanks a ton! Here's what I'm working with:
Setup Details:
- Next.js version: 15.1.3
- gRPC-Web version: 1.5.2
- Frontend usage: Running on the browser
- Communication file:
- Proto files:
- Backend server: Hosted locally at
The Problem:
When I run the frontend with
./node_modules/@grpc/grpc-js/build/src/resolver-dns.js:20:15
Module not found: Can't resolve 'dns'
18 | exports.setup = exports.DEFAULT_PORT = void 0;
19 | const resolver_1 = require("./resolver");
21 | const service_config_1 = require("./service-config");
22 | const constants_1 = require("./constants");
23 | const metadata_1 = require("./metadata");
Here is the snippet of
import { RPRaisingServiceClient } from "@/proto/generated/rpraising_grpc_pb";
const GRPC_SERVER_ADDRESS = "http://localhost:50051";
// Create a gRPC client
const client = new RPRaisingServiceClient(GRPC_SERVER_ADDRESS);
// GenerateRP RPC
export const generateRP = async (formData: FormData): Promise<GenerateRPResponse> => {
const request = new GenerateRPRequest();
...
return new Promise((resolve, reject) => {
client.generateRP(request, {}, (error, response) => {
if (error) {
console.error("Error in GenerateRP:", error);
reject(error);
} else {
resolve(response);
}
});
});
};
Setup Details:
- Next.js version: 15.1.3
- gRPC-Web version: 1.5.2
- Frontend usage: Running on the browser
- Communication file:
utils/api.ts connects to the gRPC server - Proto files:
.d.ts and pb.d.ts are located in proto/generated/ and were generated by protoc- Backend server: Hosted locally at
localhost:50051The Problem:
When I run the frontend with
npm run dev, I get this error:./node_modules/@grpc/grpc-js/build/src/resolver-dns.js:20:15
Module not found: Can't resolve 'dns'
18 | exports.setup = exports.DEFAULT_PORT = void 0;
19 | const resolver_1 = require("./resolver");
20 | const dns_1 = require("dns");| ^^^^^^^^^^^^^^
21 | const service_config_1 = require("./service-config");
22 | const constants_1 = require("./constants");
23 | const metadata_1 = require("./metadata");
Here is the snippet of
api.ts file currently looks like this:import { RPRaisingServiceClient } from "@/proto/generated/rpraising_grpc_pb";
const GRPC_SERVER_ADDRESS = "http://localhost:50051";
// Create a gRPC client
const client = new RPRaisingServiceClient(GRPC_SERVER_ADDRESS);
// GenerateRP RPC
export const generateRP = async (formData: FormData): Promise<GenerateRPResponse> => {
const request = new GenerateRPRequest();
...
return new Promise((resolve, reject) => {
client.generateRP(request, {}, (error, response) => {
if (error) {
console.error("Error in GenerateRP:", error);
reject(error);
} else {
resolve(response);
}
});
});
};
9 Replies
American Chinchilla
looks like dns package does not exist in your project
@American Chinchilla looks like dns package does not exist in your project
Ojos AzulesOP
how do i go about this then? should i install the dns-browserify package? sorry for the noob questions, i'm very new to this :/
@American Chinchilla looks like dns package does not exist in your project
Ojos AzulesOP
and should i use grpc-js or grpc-web for my next.js frontend?
@Ojos Azules and should i use grpc-js or grpc-web for my next.js frontend?
American Chinchilla
I think web would be better since it appears to be specifically for web clients
@American Chinchilla I think web would be better since it appears to be specifically for web clients
Ojos AzulesOP
im currently struggling to install grpc-web in my dependencies:
From my understanding to generate the necessary typescript files, I need to have a working version of protoc and ts-protoc-gen or protoc-gen-ts. but when I type in which protoc-gen-ts nothing comes out.
how did you install protoc-gen-ts correctly?
From my understanding to generate the necessary typescript files, I need to have a working version of protoc and ts-protoc-gen or protoc-gen-ts. but when I type in which protoc-gen-ts nothing comes out.
how did you install protoc-gen-ts correctly?
American Chinchilla
i have never touched grpc libraries in my life lol
@American Chinchilla i have never touched grpc libraries in my life lol
Ojos AzulesOP
oh man this is such a hassle man
American Chinchilla
only thing i can recommend is to check their docs, which i assume you already have haha
https://github.com/grpc/grpc-web
https://github.com/grpc/grpc-web
@American Chinchilla only thing i can recommend is to check their docs, which i assume you already have haha
https://github.com/grpc/grpc-web
Ojos AzulesOP
yes i really did and its getting frustrating, by a lot