Home > Enterprise >  How to create a nodejs microservices project in intellij?
How to create a nodejs microservices project in intellij?

Time:09-19

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? I need to start multiple services together and obviously debug any in a convenient fashion if a possibility exists.

CodePudding user response:

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.

  • Related