在 Mac 操作系统上找不到 Angular CLI ng 命令

pix*_*xel 21 macos angular-cli

我查看了此处有关此问题的大量帖子并尝试了它们,但没有成功解决此问题。

我在 MacOS 上,这是我迄今为止根据我在此处找到的建议所做的工作,但我仍然收到此错误

 ~~ sudo npm uninstall -g angular-cli

 ~~ sudo npm uninstall -g @angular/cli

 ~~ sudo npm cache clean --force

 ~~ sudo npm install -g @angular/cli
Run Code Online (Sandbox Code Playgroud)

这输出:

/usr/local/Cellar/node/11.10.0/bin/ng -> /usr/local/Cellar/node/11.10.0/lib/node_modules/@angular/cli/bin/ng

> @angular/cli@8.3.6 postinstall /usr/local/Cellar/node/11.10.0/lib/node_modules/@angular/cli
> node ./bin/postinstall/script.js

+ @angular/cli@8.3.6
added 245 packages from 185 contributors in 8.784s
Run Code Online (Sandbox Code Playgroud)

但是,发出以下命令不起作用:

 ~~ ng version

-bash: ng: command not found
Run Code Online (Sandbox Code Playgroud)

有些人建议链接,所以我也尝试过:

 ~~ sudo npm link @angular/cli
Run Code Online (Sandbox Code Playgroud)

,输出如下:

/Users/dinob/node_modules/@angular/cli -> /usr/local/Cellar/node/11.10.0/lib/node_modules/@angular/cli
Run Code Online (Sandbox Code Playgroud)

,但 ng 版本仍然无法正常工作:

 ~~ ng version

-bash: ng: command not found
Run Code Online (Sandbox Code Playgroud)

许多帖子建议.npm-global在我的 /Users/dinob 目录下创建一个目录,但我没有看到它。我只看到.npm目录,而不是.npm-global.

我还尝试了以下操作:

如上所述卸载angular

酿造更新

brew upgrade node // 从 11.10.0 升级到 12.10.0

然后重复上面的步骤来安装 angular/cli

还是一样的问题,找不到ng命令

这不是 KenWhite 建议的重复问题,我已经查看了关于这个问题的所有帖子,所以我可以找到(以及更多)关于这个问题的帖子,尝试了它们,但没有一个为我解决了这个问题。

sudo npm install -g @angular/cli 命令完成并返回以下路径,但其中没有@angular 目录:

/usr/local/Cellar/node/11.10.0/bin/ng -> /usr/local/Cellar/node/11.10.0/lib/node_modules/@angular/cli/bin/ng 
Run Code Online (Sandbox Code Playgroud)

上面,没有bin文件夹:

dinob @ /usr/local/Cellar/node/11.10.0 
 ~~ ls -la

total 80
drwxr-xr-x  8 dinob  staff    256  2 Oct 11:30 ./
drwxr-xr-x  5 dinob  staff    160 27 Sep 09:29 ../
-rw-r--r--@ 1 dinob  staff   8196  2 Oct 11:32 .DS_Store
-rw-r--r--  1 dinob  staff  26696 14 Feb  2019 README.md
drwxr-xr-x  3 dinob  staff     96 14 Feb  2019 etc/
drwxr-xr-x  3 dinob  staff     96 14 Feb  2019 include/
drwxr-xr-x  5 dinob  staff    160  2 Oct 11:22 lib/
drwxr-xr-x  5 dinob  staff    160 14 Feb  2019 share/
Run Code Online (Sandbox Code Playgroud)

此位置相同> @angular/cli@8.3.6 postinstall /usr/local/Cellar/node/11.10.0/lib/node_modules/@angular/cli

dinob @ /usr/local/Cellar/node/11.10.0/lib/node_modules 
 ~~ ls -la

total 16
drwxr-xr-x   6 dinob  staff   192  2 Oct 11:22 ./
drwxr-xr-x   5 dinob  staff   160  2 Oct 11:22 ../
-rw-r--r--@  1 dinob  staff  6148  2 Oct 11:27 .DS_Store
drwxr-xr-x   7 root   staff   224 26 Sep 16:42 n/
drwxr-xr-x  26 dinob  staff   832  2 Oct 11:28 npm/
drwxr-xr-x   6 dinob  staff   192 15 Jul 16:32 react-native-cli/
Run Code Online (Sandbox Code Playgroud)

pix*_*xel 30

经过几天的谷歌搜索,在这里和@Angular github 都没有得到任何帮助,这几乎毫无用处,终于能够解决这个问题,并按照以下步骤解决了我的 angular ng 命令未找到问题:

1.安装nvm

发出这 3 个命令来安装 nvm。(用于设置环境的Angular 文档步骤https://angular.io/guide/setup-local对我不起作用)。

所以我像这样安装了 nvm:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

export NVM_DIR="/Users/your-user-name/.nvm"

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Run Code Online (Sandbox Code Playgroud)

在此之后,请确保您重新启动终端,您应该能够发出nvm --version以查看已安装 nvm 的版本。

2.使用nvm安装节点

nvm install stable

nvm install node
Run Code Online (Sandbox Code Playgroud)

3.最后,安装angular

npm install -g @angular/cli
Run Code Online (Sandbox Code Playgroud)

4.重启终端

重新启动终端,您应该可以使用ng version查看系统上安装的版本

 ~~ ng version


     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / ? \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 8.3.6
Node: 12.11.1
OS: darwin x64
Angular: 
... 

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.803.6
@angular-devkit/core         8.3.6
@angular-devkit/schematics   8.3.6
@schematics/angular          8.3.6
@schematics/update           0.803.6
rxjs                         6.4.0
Run Code Online (Sandbox Code Playgroud)

我现在可以创建并开始我的项目

ng new my-test-project

ng serve my-test project
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我认为 SO 应该开始认真对待人们拒绝投票的问题或在阅读它们之前将它们标记为重复的问题,甚至试图了解问的是什么以及问题是什么。

似乎有很多人阅读标题并根据此决定问题的命运,任何分歧最终都会完全阻止问题。

一个本应提供帮助的网站上存在如此多的偏见和仇恨。

非常需要更好的替代品。

更新

如果您像我一样改用 zsh shell 而不是 bash shell(因为 Catalina MacOs 现在使用 zsh),您可能已经注意到它对您ng version不起作用。

在这种情况下,通过在 vim 中打开它来修改 .zshrc 文件:

vi ~/.zshrc

在那里,找到这一行:

source $ZSH/oh-my-zsh.sh
Run Code Online (Sandbox Code Playgroud)

在此行下方,添加以下行:

source /Users/Your-User-Name/.bash_profile
Run Code Online (Sandbox Code Playgroud)

通过按 Esc 键并键入:wq并按 Enter来保存文件

重启你的终端

重新发布ng version,您应该会在 zsh shell 中看到它:

在此处输入图片说明

  • 我100%同意。您永远不知道某人会如何措辞搜索,也不知道结果将如何出现。我很高兴你回答得这么好。 (2认同)

Arj*_*ava 21

如果以上所有方法都不起作用,请angular-cli使用 Homebrew安装。

在您的终端 (ZSH) 中运行:

brew install angular-cli
Run Code Online (Sandbox Code Playgroud)

喝杯咖啡,回来并运行它来测试。

ng --version
Run Code Online (Sandbox Code Playgroud)

答对了。

  • 对我来说是“brew install angular-cli” (3认同)

Mos*_*nia 8

最好的解决办法是

alias ng="/Users/YourName/.npm-global/bin/ng"
Run Code Online (Sandbox Code Playgroud)