Making a local npm package is relatively easy - you create a new Node.js project.
npm init
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 = {
<service>Messages,
<service>Services
};
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>
And the package.json
will have:
"<name of package>": "file:<path to package>"
Top comments (0)