If you haven't installed the Nx CLI globally yet, run the following command:
npm install -g nxIf you don't already have a workspace, create one using:
npx create-nx-workspace@latest microservices-project
cd microservices-projectOpen apps directory:
cd appsThen, generate a new application:
nx generate @nx/node:application api-gateway --dockerYou can check the structure of your new workspace using:
tree -I node_modulesTo set up NestJS in your project, install the following dependencies:
dependencies
npm install @nestjs/common @nestjs/core @nestjs/microservices @nestjs/platform-express rxjs reflect-metadataInstall the development dependencies:
devDependencies
npm install --save-dev @nestjs/cli @nestjs/schematics @nestjs/testing @nx/nestTo generate a new service like api-gateway:
nx generate @nx/node:application api-gateway --dockerInstall the NestJS plugin for Nx:
npm install @nrwl/nest --save-devThen, navigate to your application directory:
cd apps/api-gatewayGenerate a pool module:
nx g @nrwl/nest:module pool --project=api-gatewayGenerate the pool controller:
nx g @nrwl/nest:controller pool --project=api-gatewayGenerate the pool service:
nx g @nrwl/nest:service pool --project=api-gatewayTo unify all modules, generate an app module:
cd apps/api-gateway/src/app
nx g @nrwl/nest:module appTo make the library buildable, use the following command:
Generate a new library/packages:
nx g @nx/nest:lib libs/my-nest-lib --buildableTo start the development server, run:
npx nx serve api.gatewayTo build the application for production, run:
npx nx build api.gatewayYou 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-upTo stop Docker containers:
make docker-downTo stop and remove all Docker containers, images, volumes, and networks:
make kill-dockersTo generate gRPC code from .proto files:
make grpc-buildmake dropdbmake createdbmake list-portsmake kill-portsTo 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 graphThis 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-gatewayTo create a production bundle:
npx nx build api-gatewayTo see all available targets to run for a project, run:
npx nx show project api-gatewayThese 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 demoTo generate a new library, use:
npx nx g @nx/node:lib mylibYou 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!