21

I am trying to create a library in Angular using nx g @nrwl/angular:lib shared but it keep giving me error zsh: command not found: nx. All I did was before just create a angular project. I try using this: npm install -g nx so maybe it install nx and it works, but I still keep getting the same error.

How can I fix this? I am using Mac. If any questions or information needed please feel free to leave the comment down below.

user022yufjb
  • 157
  • 1
  • 3
  • 11

7 Answers7

17

sudo npm install -g nx fixed, sudo might be needed...

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
  • Doing sudo npm i is usally a bad idea. There are many reasons like you can potentially mess up your environment, and it's usually a pain to fix if that happens. There are also security concerns. There was a trend where malicious packages were created with misspellings of well known packages like `loadash` (instead of properly spelled `lodash`). At the very least I'd recommend you try first without `sudo`. Here is an [article with more info](https://medium.com/@ExplosionPills/dont-use-sudo-with-npm-5711d2726aa3) – Felipe Centeno Apr 19 '23 at 16:26
13

Try this: Install it globally by below command 100% will work

sudo npm i -g @nrwl/cli

remove -g if you don't want to install globally.

Mahaveer
  • 169
  • 1
  • 9
  • Doing sudo npm i is usally a bad idea. There are many reasons like you can potentially mess up your environment, and it's usually a pain to fix if that happens. There are also security concerns. There was a trend where malicious packages were created with misspellings of well known packages like `loadash` (instead of properly spelled `lodash`). At the very least I'd recommend you try first without `sudo`. Here is an [article with more info](https://medium.com/@ExplosionPills/dont-use-sudo-with-npm-5711d2726aa3) – Felipe Centeno Apr 19 '23 at 16:26
13

Do we really need to install @nrwl/cli ?

Just installing the nx resolve the issue for me

sudo npm install -g nx@latest
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • 1
    In 2023 this should be the correct answer! As you can see [here](https://github.com/nrwl/nx/issues/9399). – Joscha Jan 25 '23 at 21:02
11

Either you don't have refreshed your zsh paths, which can be fixed by opening a new terminal or type

rehash

in the prompt.

If this doesn't work then nx isn't globally installed on your machine. You have to type npm run nx [your command] instead of just "nx" to use it, so in your case:

npm run nx -- g @nrwl/angular:lib shared

Sources: https://nx.dev/latest/angular/cli/overview#installing-the-cli

rensothearin
  • 670
  • 1
  • 5
  • 24
Ruapsin
  • 126
  • 1
  • 3
  • The second option worked. I was wondering how can I install it globally, so I don't have to type run? – user022yufjb Nov 30 '20 at 07:27
  • Also, can you answer my this question as well? Question link: https://stackoverflow.com/questions/65068124/how-to-create-an-application-in-angular-project – user022yufjb Nov 30 '20 at 07:31
3

Installing the nrwl cli globally should do the trick, you'll need to enter the super user mode to do it.

sudo npm i -g @nrwl/cli

Alternative solutions can be found on the guide on the Nx Website here

https://nx.dev/l/a/tutorial/01-create-application

superdwale
  • 37
  • 4
0

Try this:

yarn global add @nrwl/cli
0

Try remove node_modules and reinstall dependencies via

npm install

or

yarn install

this works for me

Cor4zon
  • 63
  • 6