Issue
This Content is from Stack Overflow. Question asked by sK500
The problem is that let’s say I want to start multiple services (several npm start
) concurrently, it would be inconvenient to run the services as separate projects. I want to have a folder structure similar to the following under one project workspace:
project
├── service1
│ ├── node_modules
│ │ ├── @module1
│ │ └── @module2
│ ├── package-lock.json
│ ├── package.json
│ ├── public
│ └── src
├── service2
│ ├── node_modules
│ │ ├── @module1
│ │ └── @module2
│ ├── package-lock.json
│ ├── package.json
│ ├── public
│ └── src
└── service3
├── node_modules
│ ├── @module1
│ └── @module2
├── package-lock.json
├── package.json
├── public
└── src
What would be a clean way to do so?
Solution
You may want to look into Lerna or Nx.
Both are tools that manage mono-repo microservices.
There are some subtle differences between them, but essentially both do the same thing.
- They offer ways to share dependencies between your microservices.
- They offer ways to created shared libraries.
- They offer ways to launch
multiple services together.
Lerna
One of the subtle differences, is that Nx will force you to use a single package.json
in your root folder, essentially forcing you to use the same dependencies for all microservices. By contrast, Lerna still allows a specific package.json
in each individual folder, which seems to resemble your current directory structure better.
In general, I think Lerna is a safe choice. And you can find a good tutorial here.
Nx
On the other hand, even though Lerna has been around for a longer time it has some quirks at times. I believe Nx is probably technically a more robust solution.
However, I must admit that I’ve mostly seen it being used for mono-repo front-end projects, and less often for back-ends. Technically, it should be able to handle both.
To get you started with Nx, you could follow this tutorial.
Spoiler: Nx has commands like nx run-many
that can help you to execute multiple services together. After migrating to nx, you could then put that command in your "start":
script of the package.json
, so that npm run start
and npm start
will execute it.
This Question was asked in StackOverflow by user19508052 and Answered by bvdb It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.