1

I am quite new to Protocol Buffers.

I noticed that the grpc proto-loader module requires just a single proto definition file to load, so I have loaded it in my code as below:

const PROTO_PATH = `${path.resolve(__dirname, '..')}${path.sep}protos${path.sep}index.proto`;
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true
});

let indexProto = grpc.loadPackageDefinition(packageDefinition).index;

Now my index.proto file is referencing another proto file as below:

syntax = "proto3";

package index;

import public "location_updater.proto";

And my location_updater.proto is defined as below

syntax = "proto3";

package location_updater;

service LocationUpdater{
    rpc updateLocation(Location) returns LocationUpdateResponse{}
}

message Location{
    string apiKey = 1;
    string updateTarget = 2;
    double longitude = 3;
    double latitude = 4;
}

message LocationUpdateResponse{
    int32 statusCode = 1;
}

When I do the following:

 let grpcServer = new grpc.Server();
        grpcServer.addService(indexProto.location_updater.LocationUpdater.service, {

        });

I am getting an error TypeError: Cannot read property 'LocationUpdater' of undefined

If I move the content of the location_updater.proto into to the index.proto file it works, but I don't want that behavior as I would be working with many different proto files for different business logic.

What am I doing wrong and what is the best way to go about this?.

Thanks in anticipation for your input.

ololo
  • 1,326
  • 2
  • 14
  • 47

1 Answers1

0

You need to use the includeDirs option to include directories in the search paths, so that the proto loader library knows how to find the imported files. Those directories should be the ones that the import paths are relative to.

In this situation, assuming that location_updater.proto is in the same directory as index.proto, the includeDirs option should be an array containing the single path __dirname/../protos. Those directories can also be used to search for the main file, so you can pass a proto path that is just index.proto.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95