Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a NodeJs microservices Architecture, should I use a package.json per service?

I'm currently developing a microservices architecture in NodeJs. My first approach, was a package.json per service. Although, it can be very tricky when accessing to a common area (with logging or database utils), for all microservices. For instance:

common-area >
    logger.js
    package.json - install module typeorm

service1 >
    app.js - use logger.js
    package.json - also install module typeorm

When running node app.js (Service 1) we end up with 2 typeorm modules loaded, once we made two different installations, one in common area (used by logger) and another in service1.

Should I use only one package.json, for all micro-services, resulting in only one node_modules folder?

like image 341
VBoss Avatar asked Apr 19 '17 14:04

VBoss


People also ask

How do you implement microservices architecture in node JS?

Directly inside the main folder, create a file called server. js . Inside, write the following code: // require express const express = require("express"); //create an app using express constructor const weatherApp = express(); // declare your port const port = 5000; // require routes from the routes.

How NodeJS microservices communicate with each other?

Building an Event-Driven Microservices Architecture One microservice will be admin connected to MySQL database, and the other will be cart connected to MongoDB. Communication between both microservices will occur using the AMQP (Advanced Message Queuing Protocol) protocol with the help of RabbitMQ.

Is package json necessary?

If you're not publishing your project to the NPM registry or otherwise making it publicly available to others, your package. json is still essential to the development flow. Your project also must include a package. json before any packages can be installed from NPM.

Is NodeJS used for microservices?

Node. js uses a modular approach to application development, allowing developers to use the microservices architecture for faster and simpler step-by-step updates. In addition, since the Node. js microservices system is loosely connected and independent, developers will have no problems with its maintenance.


1 Answers

Should I use only one package.json, for all micro-services, resulting >in only one node_modules folder?

You shouldn't worry about having multiple node_modules directories when you're doing microservices because one of the points of microservices is to be able to put them on separate hosts or in separate containers and they won't be able to share their dependencies anyway.

Every microservice should be a separate service. You should avoid tight coupling and possible leaky abstractions that can result from fighting against the microservice architecture and joining them together.

Of course microservices is not the only architecture that works in practice but you want microservices then every microservice should be completely independent. Otherwise it's not microservices.

If there is any common code that you can share among the services then put it in a module that would be required by all or some of the services as needed. You can keep private modules on npm, you can host a private npm registry, or you can install that module directly from GitHub or GitLab private repos. All you need to do to add a private (or public) module hosted on GitHub would be to run:

npm install user/repo --save

where user is your user (or organization) name on GitHub and repo is the name of the repository. Keep in mind that to install such a module if it's in a private repo the ssh key used on that machine would have to belong to a user that has read access to the repo. You can also use deploy keys on GitHub for more flexibility.

You can even simplify loading external dependencies in your services by creating a separate module that includes all of the dependencies and exposes them to your services all at once. E.g. if you create a module called dependencies that looks like this:

module.exports = {
  _: require('lodash'),
  P: require('bluebird'),
  fs: require('mz/fs'),
  // ...
};

Then you can use all of those modules in your microservices like this:

const { _, P, fs } = require('dependencies');

When you have a large number of small microservices, especially when those are split into multiple files, this can simplify things quite a bit.

like image 163
rsp Avatar answered Nov 15 '22 08:11

rsp