Skip to main content

NX Libraries

With NX most of the business and domain logic should reside inside libraries that can also be published separately from the apps.

Reasons of creating NX Libraries are:

  • Faster Commands
  • Architecture Visualisation
  • Enforcing Constraints

See best practice lib example

Types

There are many different types of libraries in a workspace, recommended types are:

  • Feature
  • UI
  • Data-access
  • Utility

Buildable and Publishable

How to group libraries

Feature

Developers should consider feature libraries as libraries that implement smart UI (with access to data sources) for specific business use cases or pages in an application.

UI

A UI library contains only presentational components (also called "dumb" components).

Data-access

A data-access library contains code for interacting with a back-end system. It also includes all the code related to state management.

Utility

A utility library contains low-level utilities used by many libraries and applications.

Creation

Refer to Generator Commands for coverage of all the options.

To create a shared library components

Decisions:

  • Audience: Shared (common) across most domains or domain (industry) specific but also want to reuse across apps.
  • Type
    • Feature (Reusable but owns data)
    • UI (For example TailwindUI components)
    • Data-access (Third Party Integrations, generic best practice patterns for API, also common models)
    • Utility (For example something like lodash, moment, etc)

Feature Library

Append -no-component if you don't need the default component.

npx nx g @nrwl/react:lib mdx-elements --directory=sharedfeature --style=css -no-component

Web data accesss

nx g lib scaffold-eth --directory=shared/data-access