How to import proto file in another proto file in NestJS

6.7k Views Asked by At

I've come across the need to import one file into another, but I can't find a clear explanation of how to do it.

So, I have my index proto file using some message from common.proto. All proto files lie in the same directory.

index.proto:

syntax = "proto3";

import "common.proto";

package index;

common.proto:

syntax = "proto3";

package common;

message Void {}

And I receive message: " Cannot resolve import 'common.proto' "

2

There are 2 best solutions below

1
On BEST ANSWER

Have you already tried to set the --proto_path flag? I tried it in my environment and what I think you are missing is telling Protobuf the location of your files

Here you can find the documentation: https://developers.google.com/protocol-buffers/docs/proto3#importing_definitions

0
On

Try loading all proto files from a directory instead, using includeDirs

An example :

app.connectMicroservice({
transport: Transport.GRPC,
options: {
  package: 'sample.user',
  protoPath: '/sample/user/user.proto',
  loader: {
    includeDirs: [join(__dirname, '..', 'protos')], // will load all proto files in the diectory 'protos'
  },
},
});