I want to build an NPM package that can be reused in Angular 18+ projects. The package should be based on .proto files. My goal is to install it using npm install @company/proto-lib and then import the generated classes in my Angular components.
Here’s my proto definition:
message Employee {
string full_name = 1;
int years_old = 2;
}
What I want to achieve in my Angular component:
import { Employee } from "@company/proto-lib";
workerData: Employee = new Employee();
workerData.SetFullName("Luke");
workerData.SetYearsOld(25);
I’m using this command to generate the files:
.\node_modules\.bin\protoc.cmd "--proto_path=.\src\protos" "--plugin=protoc-gen-ts=.\node_modules\.bin\protoc-gen-ts.cmd" "--ts_out=service=grpc-web:.\output" "--js_out=import_style=commonjs,binary:.\output" ".\src\protos\Employee.proto"
This creates Employee_pb.d.ts and Employee_pb.js files in my output directory.
My package.json looks like this:
{
"name": "@company/proto-lib",
"version": "1.0.0",
"main": "output/Employee_pb.js",
"files": [
"output/",
"output/Employee_pb.js",
"output/Employee_pb.d.ts"
],
"type": "module",
"dependencies": {
"@improbable-eng/grpc-web": "^0.15.0",
"@protobuf-ts/protoc": "2.9.4",
"ts-protoc-gen": "0.15.0"
}
}
The problem is that after publishing with npm pack and npm publish, when I install the package and try to use it, the imported class shows as undefined in the console. However, TypeScript autocomplete works fine and the types are recognized correctly.
When I test the same files locally (not through NPM), everything works as expected. I think there might be an issue with how webpack handles the module resolution for the packaged files.
Any ideas on how to fix this module resolution issue?