Nx Configuration
The primary Nx configuration lives innx.json at the repository root:
nx.json
Key Concepts
Named Inputs
Named inputs define file sets that Nx uses to determine if a task needs to be re-run:default
default
All files in the project root plus shared global files:
production
production
All default files excluding test files:Used for production builds to avoid cache invalidation from test changes.
sharedGlobals
sharedGlobals
Target Defaults
Target defaults apply configuration to all projects with a matching target:"dependsOn": ["^build"]- Build all dependencies before building this project (the^means upstream dependencies)"inputs"- Use production files for cache key calculation"outputs"- Cache the build output directory"cache": true- Enable Nx computation caching
Project Configuration
Each library has its ownproject.json defining available targets and configuration:
libs/auth/project.json
Project Tags
Tags enable enforcement of architectural boundaries:.eslintrc.js
Running Tasks
Single Project
Run a target for a specific project:Multiple Projects
Run a target across multiple projects:Affected Projects
Run tasks only for projects affected by changes:How “affected” works:Nx analyzes your git changes and the dependency graph to determine which projects are affected. If you modify
libs/auth, Nx knows that apps/web, apps/browser, apps/desktop, and apps/cli depend on it and marks them as affected.Parallel Execution
Nx can run tasks in parallel for better performance:nx.json
Computation Caching
Nx caches task outputs to avoid redundant work. When you run a task, Nx:-
Computes a hash based on:
- Input files (defined by
inputs) - Task configuration
- Dependency outputs (if
dependsOnspecified)
- Input files (defined by
- Checks the cache for matching hash
- Restores from cache if found, or runs the task and caches the result
Cache Directory
Cached outputs are stored in.nx/cache:
Clearing Cache
Skip Cache
Force execution even if cache exists:Task Dependencies
Define task execution order withdependsOn:
Upstream Dependencies (^)
apps/web depends on libs/auth, running nx build web will first build @bitwarden/auth.
Same-Project Dependencies
generate-types target in the same project before build.
Combined Dependencies
generate-types locally AND build upstream dependencies.
Custom Plugins
The repository includes a custom Nx plugin:nx.json
libs/nx-plugin and provides:
- Custom executors for Bitwarden-specific build tasks
- Generators for creating new libraries and applications
- Additional linting rules and checks
libs/nx-plugin/README.md for details.
Task Pipelines
Nx automatically creates a task pipeline based on dependencies: Runningnx build web executes:
nx build @bitwarden/commonnx build @bitwarden/authandnx build @bitwarden/vault(parallel)nx build web
Visualizing the Graph
Nx can generate a visual dependency graph:- All projects in the workspace
- Dependencies between projects
- Affected projects (when using
affected:graph)
Common Workflows
Development Workflow
CI/CD Workflow
Full Rebuild
Performance Optimization
Use Affected Commands
Only build/test what changed:
nx affected -t testEnable Caching
Ensure
cache: true in target definitionsIncrease Parallelism
Use
--parallel=8 for CI environments with more CPU coresOptimize Inputs
Exclude unnecessary files from
inputs to improve cache hit rateIntegration with Angular
Angular projects useangular.json alongside Nx configuration:
angular.json
Troubleshooting
Cache not being used
Cache not being used
Symptoms: Tasks always run even when nothing changed.Solutions:
- Verify
cache: truein target configuration - Check that
inputsare defined correctly - Run
nx resetto clear corrupted cache - Ensure timestamps are stable (watch for tools that modify files)
Dependency graph incorrect
Dependency graph incorrect
Symptoms: Affected detection misses dependencies or includes too much.Solutions:
- Run
nx graphto visualize actual dependencies - Check
tsconfig.base.jsonpath mappings are correct - Verify imports use path aliases, not relative paths
- Update
nx.jsonif you’ve added new projects
Tasks running in wrong order
Tasks running in wrong order
Symptoms: Build fails because dependencies aren’t built first.Solutions:
- Add
"dependsOn": ["^build"]to target configuration - Check that dependency relationships are correct in code
- Verify
project.jsonfiles have correct dependencies
Best Practices
Keep tasks cacheable
Avoid non-deterministic operations (timestamps, random values) in build outputs
Next Steps
Dependency Injection
Learn how services are registered and injected
Monorepo Structure
Understand the workspace organization