Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import proto file in another proto file in NestJS

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' "

like image 727
wh4y Avatar asked Oct 25 '25 02:10

wh4y


2 Answers

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

like image 94
Anditthas Avatar answered Oct 26 '25 17:10

Anditthas


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'
  },
},
});
like image 45
bareMetal Avatar answered Oct 26 '25 17:10

bareMetal