If you haven't installed the Nx CLI globally yet, run the following command:
npm install -g nx
If you don't already have a workspace, create one using:
npx create-nx-workspace@latest microservices-project
cd microservices-project
Open apps directory:
cd apps
Then, generate a new application:
nx generate @nx/node:application api-gateway --docker
You can check the structure of your new workspace using:
tree -I node_modules
To set up NestJS in your project, install the following dependencies:
dependencies
npm install @nestjs/common @nestjs/core @nestjs/microservices @nestjs/platform-express rxjs reflect-metadata
Install the development dependencies:
devDependencies
npm install --save-dev @nestjs/cli @nestjs/schematics @nestjs/testing @nx/nest
To generate a new service like api-gateway
:
nx generate @nx/node:application api-gateway --docker
Install the NestJS plugin for Nx:
npm install @nrwl/nest --save-dev
Then, navigate to your application directory:
cd apps/api-gateway
Generate a pool
module:
nx g @nrwl/nest:module pool --project=api-gateway
Generate the pool
controller:
nx g @nrwl/nest:controller pool --project=api-gateway
Generate the pool
service:
nx g @nrwl/nest:service pool --project=api-gateway
To unify all modules, generate an app module:
cd apps/api-gateway/src/app
nx g @nrwl/nest:module app
To make the library buildable, use the following command:
Generate a new library/packages:
nx g @nx/nest:lib libs/my-nest-lib --buildable
To start the development server, run:
npx nx serve api.gateway
To build the application for production, run:
npx nx build api.gateway
You can use Nx to run various tasks for your applications.
npx nx <target> <project> <...options>
npx nx run-many -t <target1> <target2>
You can also filter the projects using -p
:
npx nx run-many -t <target1> <target2> -p <proj1> <proj2>
To start and build Docker containers:
make docker-up
To stop Docker containers:
make docker-down
To stop and remove all Docker containers, images, volumes, and networks:
make kill-dockers
To generate gRPC code from .proto
files:
make grpc-build
make dropdb
make createdb
make list-ports
make kill-ports
To listen to a Kafka topic:
make kcat TOPIC=<topic-name>
- Set up Nx Cloud to share your workspace's cache and distribute tasks across multiple machines.
To explore the project graph and task dependencies:
nx graph
This project includes several Makefile commands to streamline common tasks.
Command | Description |
---|---|
make run-all |
Run all projects using the Nx CLI |
make run PROJECT=<name> |
Run a single project using the Nx CLI |
make grpc-build |
Generate gRPC code from .proto files |
make docker-up |
Run Docker Compose up |
make docker-down |
Run Docker Compose down |
make kill-dockers |
Kill and remove all Docker resources |
make nx-reset |
Reset Nx cache and daemon |
make kill PORT=<number> |
Kill the process running on the specified port |
make dropdb |
Drop the PostgreSQL database |
make createdb |
Create the PostgreSQL database |
make kcat TOPIC=<name> |
Listen to a Kafka topic using kcat |
make tree |
Show the project structure |
make generate-schema SCHEMA=<name> |
Generate and apply schema from DBML |
make dbdocs-build SCHEMA=<name> |
Generate DBML documentation |
make list-ports |
List processes running on the specified ports |
make kill-ports |
Kill processes running on the specified ports |
Click here to finish setting up your workspace!
To run the dev server for your app, use:
npx nx serve api-gateway
To create a production bundle:
npx nx build api-gateway
To see all available targets to run for a project, run:
npx nx show project api-gateway
These targets are either inferred automatically or defined in the project.json
or package.json
files.
More about running tasks in the docs »
While you could add new projects to your workspace manually, you might want to leverage Nx plugins and their code generation feature.
Use the plugin's generator to create new projects.
To generate a new application, use:
npx nx g @nx/node:app demo
To generate a new library, use:
npx nx g @nx/node:lib mylib
You can use npx nx list
to get a list of installed plugins. Then, run npx nx list <plugin-name>
to learn about more specific capabilities of a particular plugin. Alternatively, install Nx Console to browse plugins and generators in your IDE.
Learn more about Nx plugins » | Browse the plugin registry »
Nx Console is an editor extension that enriches your developer experience. It lets you run tasks, generate code, and improves code autocompletion in your IDE. It is available for VSCode and IntelliJ.
This project is designed to run on Linux Ubuntu. Please give it a star ⭐ if you find it helpful!