Migrating an Angular CLI project to Nx
Within an Nx workspace, you gain many capabilities that help you build applications and libraries. If you are currently using an Angular CLI workspace, you can transform it into an Nx workspace.
Migrating to a Standalone Angular App with Nx
You can migrate to a standalone Angular app with the command:
โฏ
npx nx@latest init
This command will install the correct version of Nx based on your Angular version.
This will enable you to use the Nx CLI in your existing Angular CLI workspace while keeping your existing file structure in place. The following changes will be made in your repo to enable Nx:
- The
nx
,@nx/workspace
andprettier
packages will be installed. - An
nx.json
file will be created in the root of your workspace. - For an Angular 14+ repo, the
angular.json
file is split into separateproject.json
files for each project.
Note: The changes will be slightly different for Angular 13 and lower.
Migrating to an Nx Monorepo
If you want to migrate your Angular CLI project to an Nx Monorepo, run the following command:
โฏ
npx nx@latest init --integrated
The command applies the following changes to your workspace:
- Installs the
nx
,@nx/angular
and@nx/workspace
packages. - Moves your applications into the
apps
folder, and updates the relevant file paths in your configuration files. - Moves your e2e suites into the
apps/<app name>-e2e
folder, and updates the relevant file paths in your configuration files. - Moves your libraries into the
libs
folder, and updates the relevant file paths in your configuration files. - Updates your
package.json
scripts to usenx
instead ofng
. - Splits your
angular.json
intoproject.json
files for each project with updated paths.
After the changes are applied, your workspace file structure should look similar to the one below:
1<workspace name>/
2โโโ apps/
3โ โโ <app name>/
4โ โโโ src/
5โ โ โโโ app/
6โ โ โโโ assets/
7โ โ โโโ favicon.ico
8โ โ โโโ index.html
9โ โ โโโ main.ts
10โ โ โโโ styles.css
11โ โโโ project.json
12โ โโโ tsconfig.app.json
13โ โโโ tsconfig.spec.json
14โโโ libs/
15โ โโโ <lib name>/
16โ โโโ src/
17โ โโโ ng-package.json
18โ โโโ package.json
19โ โโโ project.json
20โ โโโ README.md
21โ โโโ tsconfig.lib.json
22โ โโโ tsconfig.lib.prod.json
23โ โโโ tsconfig.spec.json
24โโโ tools/
25โโโ .editorconfig
26โโโ .gitignore
27โโโ .prettierignore
28โโโ .prettierrc
29โโโ karma.conf.js
30โโโ nx.json
31โโโ package.json
32โโโ README.md
33โโโ tsconfig.base.json
34
Modified Folder Structure
The automated migration supports Angular CLI workspaces with a standard structure, configurations and features. It supports workspaces using the following executors (builders):
@angular-devkit/build-angular:application
@angular-devkit/build-angular:browser
@angular-devkit/build-angular:browser-esbuild
@angular-devkit/build-angular:dev-server
@angular-devkit/build-angular:extract-i18n
@angular-devkit/build-angular:karma
@angular-devkit/build-angular:ng-packagr
@angular-devkit/build-angular:prerender
@angular-devkit/build-angular:protractor
@angular-devkit/build-angular:server
@angular-devkit/build-angular:ssr-dev-server
@angular-eslint/builder:lint
@cypress/schematic:cypress
@nguniversal/builders:prerender
@nguniversal/builders:ssr-dev-server
Support for other executors may be added in the future.
After migration
Your workspace is now powered by Nx! You can verify that your application still runs as intended:
- To serve, run
nx serve <app name>
. - To build, run
nx build <app name>
. - To run unit tests, run
nx test <app name>
. - To see your project graph, run
nx graph
.
Your project graph will grow as you add and use more applications and libraries. You can add the
--watch
flag tonx graph
to see the changes in-browser as you add them.
Set Up CI for Your Angular Workspace
This tutorial walked you through how Nx can improve the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution.
- Nx reduces wasted time in CI with the
affected
command. - Nx Replay's remote caching will reuse task artifacts from different CI executions making sure you will never run the same computation twice.
- Nx Agents efficiently distribute tasks across machines ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute.
- Nx Atomizer automatically splits large e2e tests to distribute them across machines. Nx can also automatically identify and rerun flaky e2e tests.
Connect to Nx Cloud
Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more.
Now that we're working on the CI pipeline, it is important for your changes to be pushed to a GitHub repository.
- Commit your existing changes with
git add . && git commit -am "updates"
- Create a new GitHub repository
- Follow GitHub's instructions to push your existing code to the repository
Now connect your repository to Nx Cloud with the following command:
โฏ
npx nx connect
A browser window will open to register your repository in your Nx Cloud account. The link is also printed to the terminal if the windows does not open, or you closed it before finishing the steps. The app will guide you to create a PR to enable Nx Cloud on your repository.
Once the PR is created, merge it into your main branch.
And make sure you pull the latest changes locally:
โฏ
git pull
You should now have an nxCloudId
property specified in the nx.json
file.
Create a CI Workflow
Use the following command to generate a CI workflow file.
โฏ
npx nx generate ci-workflow --ci=github
This generator creates a .github/workflows/ci.yml
file that contains a CI pipeline that will run the lint
, test
, build
and e2e
tasks for projects that are affected by any given PR. Since we are using Nx Cloud, the pipeline will also distribute tasks across multiple machines to ensure fast and reliable CI runs.
The key lines in the CI pipeline are:
1name: CI
2# ...
3jobs:
4 main:
5 runs-on: ubuntu-latest
6 steps:
7 - uses: actions/checkout@v4
8 with:
9 fetch-depth: 0
10 # This enables task distribution via Nx Cloud
11 # Run this command as early as possible, before dependencies are installed
12 # Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun
13 # Connect your workspace by running "nx connect" and uncomment this
14 - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build"
15 - uses: actions/setup-node@v3
16 with:
17 node-version: 20
18 cache: 'npm'
19 - run: npm ci --legacy-peer-deps
20 - uses: nrwl/nx-set-shas@v4
21 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
22 - run: npx nx affected -t lint test build
23
Open a Pull Request
Commit the changes and open a new PR on GitHub.
โฏ
git add .
โฏ
git commit -m 'add CI workflow file'
โฏ
git push origin add-workflow
When you view the PR on GitHub, you will see a comment from Nx Cloud that reports on the status of the CI run.
The See all runs
link goes to a page with the progress and results of tasks that were run in the CI pipeline.
For more information about how Nx can improve your CI pipeline, check out one of these detailed tutorials:
Learn More
Learn more about the advantages of Nx in the following guides:
- Using Cypress for e2e tests
- Using Jest for unit tests
- Computation Caching
- Rebuilding and Retesting What is Affected
- Integrate with Editors
- Advanced Angular Micro Frontends with Dynamic Module Federation
From Nx Console
Nx Console no longer supports the Angular CLI. Angular CLI users will receive a notice, asking if they want to switch to Nx. When you click this button, weโll run the nx init
command to set up the Nx CLI, allowing for cached builds, and for you to share this cache with your teammates via Nx Cloud.
If you're not ready to make the change yet, you can come back to this later:
- If you're using Nx Console: open the Vs Code command palette and start typing "Convert Angular CLI to Nx Workspace".
- Regardless of using Nx Console (or your IDE): run
npx nx init
from the root of your project.
Once the script has run, commit the changes. Reverting this commit will effectively undo the changes made.