DEV Community


Creating a Local npm Package for Protobuf Files

I am a U.S. Army veteran turned full-stack JavaScript developer.
・1 min read

Making a local npm package is relatively easy - you create a new Node.js project.

npm init
Enter fullscreen mode Exit fullscreen mode

In my case, the sole purpose of this package is to deliver generated protobuf files to various gRPC servers and clients.

I made my entry point main.js and used it to export references to the protobuf files. Since protoc creates two files - one for the messages and one for the services - I created a created a variable for each with the naming pattern of <service>Messages and <service>Services.

const <service>Messages = require(`<name of service>_pb.js`);
const <service>Services = require(`<name of service>_grpc_pb.js);

module.exports = {
Enter fullscreen mode Exit fullscreen mode

This pattern makes it very easy to import the code I need into my servers and clients. Also, any of my co-workers needing to consume the packages will not need to guess as to what the name of the exports are.

I can now install these packages as part of the service, and they will update themselves as the package updates.

npm install --save <path to module>
Enter fullscreen mode Exit fullscreen mode

And the package.json will have:

"<name of package>": "file:<path to package>"
Enter fullscreen mode Exit fullscreen mode

Discussion (0)