Andrew's Digital Garden

Apps and libs in NX

NX workspaces are structured into applications (apps) and libraries (libs). Applications are 'containers' that link, bundle, and compile libraries into a deployed app. Libraries hold a lot of that functionality, and are usually not published directly.

The mental model is the 80/20 approach. 80% of logic in libraries, 20% in apps.

Libraries don't need to be general purpose and used across applications. They can exist purely as a code-organisation tool. NX recommends exporting everything from a single index.ts file, which pushes an 'API thinking' approach.

Libraries are usually split based on domains, but can be split based on anything. Having nested libraries is also valid, which can be used for things like 'app-specific' nested libraries. Avoid creating catch-all libraries, as they often become ownerless and unmaintained.

NX recommends four different library types: feature, UI, data-access, and utility.

Remember that like all folder structures, related code should be close together, and sharing doesn't have to be done immediately. Remember the [[20211115112656-rule-of-three]]. You don't have to get it right the first time. It's easy to move things around and re-architect as needed.

There are also tags, which can be used to enforce project boundaries.

If this doc gets too long, can separate it

https://nx.dev/more-concepts/applications-and-libraries

[[architecture]] [[nx]]

Referred in

Apps and libs in NX