Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
grpc
✓ import grpc from 'grpc';
✗ const grpc = require('grpc');
CommonJS require is still widely used with grpc package, but ESM import is available. Ensure your Node version supports ESM (>=12) if using import.
protoLoader
✓ import protoLoader from '@grpc/proto-loader';
✗ import { loadSync } from '@grpc/proto-loader';
The default export is a function; named exports exist but are not the primary API.
loadSync
✓ import { loadSync } from '@grpc/proto-loader';
✗ import protoLoader, { loadSync } from '@grpc/proto-loader';
You can import both default and named, but the default is the same function. Best to use named imports for clarity.
Sets up a basic gRPC server with a single RPC method (sayHello) and a corresponding client that calls it.
// server.js
import grpc from 'grpc';
import protoLoader from '@grpc/proto-loader';
const packageDefinition = protoLoader.loadSync('helloworld.proto', {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
function sayHello(call, callback) {
callback(null, { message: 'Hello ' + call.request.name });
}
const server = new grpc.Server();
server.addService(helloProto.Greeter.service, { sayHello: sayHello });
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure());
server.start();
console.log('Server running on port 50051');
// client.js
import grpc from 'grpc';
import protoLoader from '@grpc/proto-loader';
const packageDefinition = protoLoader.loadSync('helloworld.proto', {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
const client = new helloProto.Greeter('localhost:50051', grpc.credentials.createInsecure());
client.sayHello({ name: 'World' }, function(err, response) {
console.log('Greeting:', response.message);
});
Errors
Common errors & fixes
Error: Cannot find module 'grpc'
The npm package `grpc` not installed.
fixRun: npm install grpc @grpc/proto-loader
Error: 14 UNAVAILABLE: DNS resolution failed
Client cannot reach server; often due to incorrect address or server not running.
fixVerify server is running and listen address is correct (e.g., 'localhost:50051').
Audit
Dependencies
grpcrequiredCore gRPC implementation for Node.js.
@grpc/proto-loaderrequiredLoads .proto files for gRPC service definitions.