NX Monorepo Project¶
The NX Monorepo Project type provides an opinionated setup for creating NX monorepo workspaces with best practices and common configurations pre-configured.
Overview¶
This project type extends the standard TypeScriptProject from projen with NX-specific features:
- NX configuration with sensible defaults and caching
- Monorepo workspace setup with package management
- GitHub Actions workflow for publishing releases
- Common project structure optimized for monorepos
- Task orchestration using NX run-many commands
- Conventional commits and automated release management
Quick Start¶
To create a new NX Monorepo project, use the projen CLI:
This command will create a new monorepo workspace with the NX template and prompt you for the required configuration options such as:
- Project name
- Default branch
- Description
After the project is created, you can customize it further by editing the .projenrc.ts file:
| .projenrc.ts | |
|---|---|
Highlighted lines explanation:
- Line 4: Choose a descriptive name for your monorepo
- Line 5: Configure the main branch name
- Line 8: Add any additional customizations supported by TypeScriptProject
Features¶
NX Configuration¶
The project automatically configures NX with optimal settings:
Task Orchestration¶
The project configures NX-optimized tasks that work across the entire monorepo:
| Task | Description | Command |
|---|---|---|
build |
Build all affected projects | nx run-many --target=build |
test |
Test all affected projects | nx run-many --target=test |
test:watch |
Watch mode testing | nx run-many --target=test:watch --skip-nx-cache |
lint |
Lint all affected projects | nx run-many --target=eslint |
package |
Package all affected projects | nx run-many --target=package |
graph |
Generate dependency graph | nx graph |
release |
Full release with conventional commits | Custom release workflow |
Workspace Structure¶
The monorepo is configured with a standard workspace structure:
my-monorepo/
├── packages/ # Individual projects/libraries
│ ├── project-a/
│ └── project-b/
├── nx.json # NX configuration
├── package.json # Workspace package.json
├── .projenrc.ts # Projen configuration
└── .github/
└── workflows/
└── publish.yml # Automated publishing
Configuration Options¶
Basic Configuration¶
All standard TypeScriptProjectOptions are supported. Here are the most common options:
Common Configuration Options¶
For Prettier and VSCode configuration options, refer to Default Configurations:
- Prettier: Customization Guide
- VSCode: Configuration Options
Inherited Configurations
The NX Monorepo project inherits all common configurations from the base TypeScriptProject. See Default Configurations for complete details.
Package Management¶
Workspace Configuration¶
The project automatically configures the workspace in package.json:
| package.json | |
|---|---|
Highlighted lines explanation:
- Line 2: Marks the root as private (not publishable)
- Line 3: Configures workspace pattern for packages
Adding Packages¶
To add new packages to your monorepo:
-
Create a new directory in the
packages/folder: -
Initialize the package with its own projen configuration:
-
Run projen to generate the package structure:
Release Management¶
Automated Publishing¶
The project includes a GitHub Actions workflow that automatically publishes packages:
| .github/workflows/publish.yml | |
|---|---|
Release Process¶
-
Create releases using conventional commits:
-
Version management with NX release:
-
Publish packages:
Development Workflow¶
Daily Development Commands¶
# Install all dependencies
yarn install
# Build all affected projects
yarn build
# Test all affected projects
yarn test
# Lint all affected projects
yarn lint
# Watch mode for testing
yarn test:watch
# View dependency graph
yarn graph
# Run custom nx commands
yarn nx [command]
Working with Affected Changes¶
NX automatically detects which projects are affected by your changes:
# Build only affected projects
yarn nx affected:build
# Test only affected projects
yarn nx affected:test
# Lint only affected projects
yarn nx affected:lint
Best Practices¶
Project Organization¶
- Keep packages focused - Each package should have a single responsibility
- Use consistent naming - Follow a clear naming convention for packages
- Shared dependencies - Put common dependencies in the root package.json
- Type definitions - Share types between packages when appropriate
Development Guidelines¶
- Conventional commits - Use conventional commit format for automatic changelog generation
- Testing strategy - Ensure each package has comprehensive tests
- Documentation - Document each package's API and usage
- Dependencies - Carefully manage dependencies to avoid conflicts
Performance Optimization¶
- Leverage NX cache - Ensure tasks are cacheable where possible
- Affected builds - Use affected commands to build only what changed
- Parallel execution - Configure tasks to run in parallel when possible
Troubleshooting¶
Common Issues¶
Cache Issues¶
Dependency Conflicts¶
Build Failures¶
Getting Help¶
- NX Documentation: https://nx.dev
- Projen Documentation: https://projen.io
- Project Issues: Report issues in the project repository
Examples¶
Complete Configuration¶
This example shows a complete configuration suitable for an enterprise monorepo with high test coverage requirements and modern TypeScript settings.