Skip to content

Nx Workspace: Comprehensive Guide for Generating Modules and Applications

Rahul R. edited this page Dec 21, 2024 · 2 revisions

Overview

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.

Reference Links


Setting Up Nx Workspace

To begin working with Nx, you first need to set up a workspace.

Command to Create a Workspace

npx create-nx-workspace@latest my-workspace

Library and Module Generation

Generating Angular Libraries

  1. Buildable Library
    A buildable library can be built independently and used across applications:

    npx nx generate @nrwl/angular:library library --buildable
  2. 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
  3. 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
  4. 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

Generating NestJS Libraries and Applications

  1. 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
  2. NestJS Application
    Create a standalone NestJS application:

    nx g @nx/nest:application api --e2eTestRunner=none

Generating Modules and Resources in NestJS

  1. Generate a Basic Module
    Add a module to a NestJS project:

    nx generate @nx/nest:module feature1 --project=my-app-core
  2. 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
  3. 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

Generating JavaScript Libraries

  1. Basic JavaScript Library
    Generate a buildable JavaScript library for shared logic:
    nx g @nx/js:lib testing --directory=packages --buildable

Generating Angular Applications and Libraries

  1. Angular Application
    Generate an Angular application:

    nx g @nx/angular:app apps/gauzyy --style=scss
  2. Angular Library
    Create a publishable Angular library:

    nx g @nx/angular:library packages/ui --publishable --importPath=@gauzy/ui
  3. Custom Application Directory
    Organize applications under specific directories:

    nx g @nx/angular:application desktopp --e2eTestRunner=none ---directory=apps

Best Practices

  1. Always Use Commands:
    Using Nx commands ensures consistent code structure and reduces manual configuration errors.

  2. Organize Libraries with Custom Directories:
    Utilize the --directory and --importPath options for better organization and reusability.

  3. Optimize Dependency Management:
    Mark libraries as buildable or publishable when necessary to improve modularity and reduce build times.

  4. 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.

Clone this wiki locally