-
Notifications
You must be signed in to change notification settings - Fork 590
Nx Workspace: Comprehensive Guide for Generating Modules and Applications
This guide provides detailed steps to generate modules, libraries, and applications using Nx commands in a monorepo environment. Nx is a smart, extensible build framework designed to manage projects efficiently by leveraging plugins and automation.
- Official Documentation: Nx Documentation
To begin working with Nx, you first need to set up a workspace.
npx create-nx-workspace@latest my-workspace
-
Buildable Library
A buildable library can be built independently and used across applications:npx nx generate @nrwl/angular:library library --buildable
-
Publishable Library
A publishable library can be published to a package registry:nx g @nx/angular:library packages/ui-core --publishable --importPath=@gauzy/ui-core
-
Custom Directory and Path
Organize libraries into custom directories and specify unique import paths:nx g @nx/angular:library --directory=packages/ui-sdk --importPath=@gauzy/ui-sdk --name=ui-sdk
-
Secondary Entry Point
Add secondary entry points to libraries for modular imports:nx g @nx/angular:library-secondary-entry-point --library=ui-core --name=i18n
-
NestJS Library
Generate a NestJS library for integration or reusable features:nx g @nx/nest:library integration-upwork --directory=packages --buildable --publishable --importPath=@gauzy/integration-upwork
-
NestJS Application
Create a standalone NestJS application:nx g @nx/nest:application api --e2eTestRunner=none
-
Generate a Basic Module
Add a module to a NestJS project:nx generate @nx/nest:module feature1 --project=my-app-core
-
Generate a CRUD Resource
Add a feature module with CRUD operations:nx generate @nx/nest:resource feature2 --project=my-app-core --type=feature --flat --crud
-
Generate Module, Controller, and Service Separately
For more granular control:nx generate @nx/nest:module feature1 --project=my-app-core --flat nx generate @nx/nest:controller feature1 --project=my-app-core --flat nx generate @nx/nest:service feature1 --project=my-app-core --flat
-
Basic JavaScript Library
Generate a buildable JavaScript library for shared logic:nx g @nx/js:lib testing --directory=packages --buildable
-
Angular Application
Generate an Angular application:nx g @nx/angular:app apps/gauzyy --style=scss
-
Angular Library
Create a publishable Angular library:nx g @nx/angular:library packages/ui --publishable --importPath=@gauzy/ui
-
Custom Application Directory
Organize applications under specific directories:nx g @nx/angular:application desktopp --e2eTestRunner=none ---directory=apps
-
Always Use Commands:
Using Nx commands ensures consistent code structure and reduces manual configuration errors. -
Organize Libraries with Custom Directories:
Utilize the--directory
and--importPath
options for better organization and reusability. -
Optimize Dependency Management:
Mark libraries asbuildable
orpublishable
when necessary to improve modularity and reduce build times. -
Leverage CRUD Generation:
Automate repetitive operations by generating CRUD modules and resources for NestJS applications.
This guide serves as a comprehensive reference for utilizing Nx commands effectively in your monorepo projects. For more advanced options and features, visit the Nx Documentation.