03 Load the proto
Both the server and client need the gRPC bindings derived from greeter.proto. Extract that work into a shared load.js:
js
// load.js
const protoLoader = require("@grpc/proto-loader");
const grpc = require("@grpc/grpc-js");
const packageDefinition = protoLoader.loadSync("greeter.proto");
const proto = grpc.loadPackageDefinition(packageDefinition).greeter;
module.exports = { grpc, proto };What happens here:
protoLoader.loadSyncreadsgreeter.protofrom disk and parses it into a plain JS object (no code generation).grpc.loadPackageDefinitionwraps that object with the gRPC runtime, giving you service constructors and service descriptors..greeterselects thepackage greeternamespace — matching thepackagedeclaration in the proto file.proto.Greeteris now a client constructor;proto.Greeter.serviceis the service descriptor used by the server.
Checkpoint — run this in your project directory (with greeter.proto present):
bash
node -e "console.log(!!require('./load').proto.Greeter)"Expected output:
trueIf you see false or an error, check that greeter.proto is in the same directory as load.js.
Next: 04 The server