node.jsprotocol-buffersgrpcenoentesmodules

Error: ENOENT: no such file or directory, open 'src/protos/user.proto'?


I'm creating a node.js gRPC service. I'm using the photo buff as an npm package in this service. so the proto buff package is structured as below.

enter image description here

and I have published this package to the GitHub npm package registry. then I used it in my user-management service as an npm package. But when I use the proto object from this library I'm receiving the following error,

Error: ENOENT: no such file or directory, open 'src/protos/user.proto'

and this is how I create a proto object

import grpc from "grpc";
import protoLoader from "@grpc/proto-loader";

var USER_PROTO_PATH = "./src/protos/user.proto";
/**
 * Package definition
 */
const userPackageDef = protoLoader.loadSync(USER_PROTO_PATH, {});
/**
 * gRPC Objects
 */
const grpcUserObject = grpc.loadPackageDefinition(userPackageDef);
/**
 * Packages
 */
export const userPackage = grpcUserObject.userPackage;

and this is my package.json

    {
  "name": "@example/protobuff",
  "version": "1.2.1",
  "description": "gRPC protocol buffer of app",
  "main": "index.js",
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/@example"
  },
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "url": "https://github.com/example/protobuff.git"
  },
  "keywords": [
    "protobuff"
  ],
  "author": "Test user",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/example/protobuff/issues"
  },
  "homepage": "https://github.com/example/protobuff#readme",
  "dependencies": {
    "@grpc/proto-loader": "^0.7.0",
    "grpc": "^1.24.11"
  }
}

what should I do to overcome this issue?


Solution

  • I was able to fix this issue by setting the proto file path as below.

    var USER_PROTO_PATH = "./node_modules/@example/protobuff/src/protos/user.proto"