NX does not handle multiple configurations: nx run app:build --configuration=production,stage-env
  • 15-May-2023
Lightrun Team
Author Lightrun Team
Share
NX does not handle multiple configurations: nx run app:build --configuration=production,stage-env

NX does not handle multiple configurations: nx run app:build –configuration=production,stage-env

Lightrun Team
Lightrun Team
15-May-2023

Explanation of the problem

 

When running commands using nx, there seems to be an issue where extra configurations are being stripped from the –configuration parameter. Specifically, it appears that nx only supports one configuration, whereas ng supports multiple configurations as of November 2019.

 

Troubleshooting with the Lightrun Developer Observability Platform

 

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for: NX does not handle multiple configurations: nx run app:build –configuration=production,stage-env

To resolve this issue, it is necessary to modify the implementation of nx so that it is not limited to only one configuration. This will allow nx to behave in the same way as ng when running commands with multiple configurations.

In order to implement this solution, the code in the run.ts file of the nx package will need to be modified. Specifically, the code at lines 46 and 120-138 will need to be changed to allow for multiple configurations to be passed to the command. Here is an example of how the modified code could look:

 

// Before modification
const configuration = parsedArgs.configuration || 'default';

// After modification
const configurations = parsedArgs.configurations || ['default'];

 

With these changes, nx should now behave the same as ng when running commands with multiple configurations.

 

Problems with nx

 

  1. Problem: Incorrect versioning of dependencies Nx can have problems with versioning of dependencies which can lead to unexpected behavior and errors. One possible solution to this problem is to ensure that all dependencies are compatible with each other by using a package manager such as Yarn or NPM. Another solution is to use the Nx version that is recommended for your Angular version.

Example:

 

$ nx build
nx run app:build
Cannot find module '@nrwl/web/babel' from '.../node_modules/@nrwl/web/plugins'

 

Solution:

 

$ npm install --save-dev @nrwl/web@latest

 

  1. Problem: Slow performance during development Nx can become slow during development, especially with large projects or many dependencies. This can make it difficult to work efficiently. One solution to this problem is to use a modern computer with a fast processor and plenty of RAM. Another solution is to use Nx’s built-in caching feature to speed up build times.

Example:

 

$ nx build
Running target "build" succeeded

$ nx build
Running target "build" succeeded

$ nx build
Running target "build" succeeded

$ nx build
Running target "build" succeeded

 

Solution:

 

$ nx build --cache

 

  1. Problem: Inconsistent behavior with ng commands Nx sometimes behaves differently than ng, which can be confusing and lead to errors. One possible solution is to stick to using ng commands instead of Nx commands if you are more comfortable with them. Another solution is to carefully read the documentation and make sure you understand the differences between ng and Nx commands.

Example:

 

$ ng generate component my-component
CREATE src/app/my-component/my-component.component.html (24 bytes)
CREATE src/app/my-component/my-component.component.spec.ts (666 bytes)
CREATE src/app/my-component/my-component.component.ts (284 bytes)
CREATE src/app/my-component/my-component.component.scss (0 bytes)
UPDATE src/app/app.module.ts (492 bytes)

$ nx generate @nrwl/angular:component my-component
? In which directory should the component be generated? apps/my-app/src/app
? What should the component be named? my-component
CREATE apps/my-app/src/app/my-component/my-component.component.ts (297 bytes)
CREATE apps/my-app/src/app/my-component/my-component.component.spec.ts (644 bytes)
CREATE apps/my-app/src/app/my-component/my-component.component.html (31 bytes)
CREATE apps/my-app/src/app/my-component/my-component.component.scss (0 bytes)
UPDATE apps/my-app/src/app/app.module.ts (661 bytes)

 

Solution: Read the documentation carefully and ensure that you are using the correct commands for your use case.

A brief introduction to nx

 

Nx is a tool designed to help developers build and manage enterprise-scale Angular applications. It provides a set of features and tools to help developers manage monorepos, streamline development workflows, and ensure consistent code quality across multiple projects. With Nx, developers can use a single CLI to create, build, test, and deploy multiple Angular applications and libraries.

Nx comes with a suite of plugins and schematics that make it easy to generate new projects, components, services, and modules. It also includes tools for generating documentation, linting code, and running automated tests. Nx’s powerful dependency graph helps developers identify and manage dependencies between projects and libraries, ensuring that changes to one component don’t break another. Additionally, Nx includes a set of tools for optimizing the performance and scalability of Angular applications, including tools for code splitting and lazy loading. Overall, Nx is a powerful tool that helps developers streamline their Angular development workflows and build high-quality applications at scale.

 

Most popular use cases for nx

 

  1. Monorepo management: nx allows developers to manage a monorepo containing multiple projects and libraries with ease. The built-in dev tools simplify the process of generating new projects, libraries, and components, and provide several schematics that automate common tasks such as generating new code or setting up CI/CD pipelines. For example, the following command generates a new Angular library in an existing nx workspace:
nx g @nrwl/angular:lib my-lib
  1. Advanced build configuration: nx offers advanced build configuration options that enable developers to optimize the build process for their specific use case. For example, developers can use the --parallel flag to run builds in parallel, which can significantly speed up build times:
nx build my-app --configuration=production --parallel
Additionally, developers can configure custom builders that integrate with other build tools or perform specialized tasks. For example, the following configuration adds a custom builder that runs a linter before building the project:
{
  "name": "lint",
  "builder": "@nrwl/linter:lint",
  "options": {
    "lintFilePatterns": ["src/**/*.ts"]
  }
}
  1. Code generation: nx provides powerful code generation tools that can speed up development and ensure consistency across projects. Developers can use the built-in schematics to generate new code, or create their own custom schematics for specialized tasks. For example, the following command generates a new component with a specified name and adds it to an existing module:
nx g @nrwl/angular:component my-component --project=my-app --module=app.module.ts
Overall, nx provides a comprehensive set of tools for managing monorepos, optimizing builds, and generating code, which can significantly improve development speed and code quality.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.