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.
generatePackageDefinition
✓ import { generatePackageDefinition } from '@grpc/grpc-js'
✗ const generatePackageDefinition = require('grpc').loadPackageDefinition
Generated files use @grpc/grpc-js, not the deprecated grpc package. The generation function is not directly exposed by this package; it's the output.
Generated service definition
✓ import { MyServiceService } from './generated/myproto_grpc_pb'
Imports are relative to the output directory specified in protoc. File names are based on proto file names with _grpc_pb suffix.
Generated message classes
✓ import { MyRequest, MyResponse } from './generated/myproto_pb'
✗ import { MyRequest } from '@grpc/grpc-js'
Message classes are generated by the companion protoc-gen-js plugin and placed in separate _pb files, not in this package.
Installs the plugin, generates gRPC service stubs from a proto file, and creates a simple gRPC server using the generated code.
// 1. Install dependencies
// npm install --save-dev protoc-gen-grpc-js google-protobuf @grpc/grpc-js
// 2. Generate code from a .proto file
// protoc --plugin=protoc-gen-grpc-js=./node_modules/.bin/protoc-gen-grpc-js \
// --js_out=import_style=commonjs,binary:./generated \
// --grpc-js_out=import_style=commonjs,binary:./generated \
// -I . myproto.proto
// 3. Use the generated code
// server.js
const grpc = require('@grpc/grpc-js');
const { GreeterService } = require('./generated/helloworld_grpc_pb');
const { HelloRequest, HelloReply } = require('./generated/helloworld_pb');
const sayHello = (call, callback) => {
const reply = new HelloReply();
reply.setMessage('Hello ' + call.request.getName());
callback(null, reply);
};
const server = new grpc.Server();
server.addService(GreeterService, { sayHello });
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
server.start();
console.log('Server running on port 50051');
});
Errors
Common errors & fixes
Error: Cannot find module 'google-protobuf'
Missing peer dependency google-protobuf at runtime.
fixRun npm install google-protobuf
Error: Invalid grpc message: Cannot read properties of undefined (reading 'serializeBinary')
Generated files are from the deprecated grpc package instead of @grpc/grpc-js.
fixEnsure protoc-gen-grpc-js (not grpc-tools) is used and the --grpc-js_out plugin is correctly specified.
Audit
Dependencies
google-protobufoptionalRequired peer dependency for generated code to work at runtime.