Add Angular Material to Nx Workspace
Asked Answered
A

10

39

I'm wanting to add angular-material to my nx-workspace.

The angular material docs (https://material.angular.io/guide/getting-started) say I should run ng add @angular/material.

I've tried that in the root of my nx workspace, as well as in an application folder I want to add it to, as well as in a lib project folder I want to add it to. In all cases I get the message The add command requires to be run in an Angular project, but a project definition could not be found.

The Angular Material docs used to include instructions on how to add dependencies manually, but I can't find that anymore. Does anyone know how to add it to projects in an Nx workspace these days?

Amp answered 5/3, 2021 at 19:19 Comment(1)
I think you need to use nx as a replacement for ng in an nx workspace. yarn nx add @angular/materialHessler
F
57

First:

Ensure you have Angular Material installed: npm i @angular/material

Then:

npx nx g @angular/material:ng-add --project=my-project-name is the command you need to run in your Nx Workspace.

Do not forget to include the project name or you will see errors.

Additionally, If you continue having errors try by answering no to the questions about Set up global Angular Material typography styles and Set up browser animations for Angular Material.

Disclaimer: Another member already answered correctly in the comments but I think a simple answer like this one should be pinned.

Fortissimo answered 26/2, 2022 at 9:26 Comment(3)
Before you can do this, you need to install @angular/material npm install -D @angular/material.Airbrush
@Airbrush Why -D? Isn't it a part of main dependencies?Irmgard
you are right, this is not correct (anymore). I think you had to install it as a dev dependency in older versions (not sure).Airbrush
S
23

If you created the an empty workspace you don't have angular.json, you will have a workspace.json, which is almost identical I think.

Assuming that you have already generated an agular app:

  1. Install the angular material lib: yarn add @angular/material

  2. To see the avaible commands from the nx console: nx list @angular/material enter image description here

  3. To add angular material to the default project: nx generate @angular/material:ng-add enter image description here

PS. Probably it will thrown an error on nx not found/installed. yarn add -g @nrwl/cli. I think is possible to add angular material to another project, didn't try that yet

https://github.com/nrwl/nx/issues/3779#issuecomment-751136586

Sextans answered 28/5, 2021 at 15:54 Comment(4)
I did what you advice and get the error Bootstrap call not found during the nx generate @angular/material:ng-add.Cobwebby
I figured it out, I needed to specify the project: nx generate @angular/material:ng-add --project=my-projectCobwebby
Can you please tell me how to add angular material icon support to Nx ?Diazonium
how to handle when I run command : nx generate @angular/material:ng-add and got peer dependencies conflict ? --force flag & --legacy-peer-deps seem not works?Marenmarena
S
17
npm install @angular/material && npx nx g @angular/material:ng-add

Then follow the same as material docs

Stlouis answered 20/1, 2022 at 17:55 Comment(0)
S
9

npm i @angular/material && npx nx g @angular/material:ng-add --project=my-project-name

That should do it.

Sporty answered 10/9, 2022 at 17:31 Comment(0)
F
5

To find out Nx angular material plugin

nx list @angular/material

To install the plugin in dev dependencies

npm install -D @angular/material

Frontolysis answered 4/7, 2022 at 13:4 Comment(0)
C
1

First you install Angular material using npm

npm install @angular/material --save

Then you run the following command:

npx nx g @angular/material:ng-add --project=your-project-name

if you don't do first step you'll get the following error:

 >  NX   Unable to resolve @angular/material:ng-add.

   Cannot find module '@angular/material/package.json'
   Require stack:
   - /workspaces/myproject/node_modules/nx/src/utils/package-json.js
   - /workspaces/myproject//node_modules/nx/src/utils/package-manager.js
   - /workspaces/myproject//node_modules/nx/bin/init-local.js
   - /workspaces/myproject//node_modules/nx/bin/nx.js
   Pass --verbose to see the stacktrace.
Colander answered 28/12, 2023 at 8:27 Comment(0)
A
0

For what it's worth I created a new Angular project (without Nx) and ran the ng add @angular/material command there, then took a look at the diff that resulted, and applied those changes in the Nx workspace and that seems to work fine. Just wondering if there is a better way.

Amp answered 5/3, 2021 at 19:55 Comment(0)
O
0

Yarn will cause build errors when using theming. I was running my build with yarn. It will complain about the @use '~@angular/material' as mat; being in a loop.

Organology answered 18/8, 2021 at 16:27 Comment(0)
M
0
yarn add -W @angular/material && yarn nx g @angular/material:ng-add

Worked for me

Moir answered 10/1, 2023 at 8:55 Comment(0)
E
0

While running the command

npm install @angular/material && npx nx g @angular/material:ng-add --project=<project_name> I faced the below issue

SchematicsException [Error]: Bootstrap call not found
    at findBootstrapModulePath (/Users/san/project/Sample/fetebird-ui/node_modules/@schematics/angular/utility/ng-ast-utils.js:67:15)
    at getAppModulePath (/Users/san/project/Sample/fetebird-ui/node_modules/@schematics/angular/utility/ng-ast-utils.js:85:32)
    at addAnimationsToNonStandaloneApp (/Users/san/project/Sample/fetebird-ui/node_modules/@angular/material/schematics/ng-add/setup-project.js:94:61)
    at /Users/san/project/Sample/fetebird-ui/node_modules/@angular/material/schematics/ng-add/setup-project.js:58:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async callRuleAsync (/Users/san/project/Sample/fetebird-ui/node_modules/@angular-devkit/schematics/src/rules/call.js:77:18)

 >  NX   Bootstrap call not found

To resolve the issue, change the below configuration in each application project.json

"targets": {
    "build": {
      "executor": "@angular-devkit/build-angular:browser",

Also, need to change the option main to bootstrap instead of main

 "options": {
        "main": "apps/host/src/bootstrap.ts",

Reference - https://github.com/nrwl/nx/issues/7621

Once done with the angular material installation, revert the changes

Edyth answered 13/5, 2023 at 3:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.