[英]Angular CLI ng command not found on Mac Os
我查看了此处有关此问题的大量帖子并尝试了它们,但没有成功解决此问题。
我在 MacOS 上,这是我迄今为止根据我在这里找到的建议所做的,但我仍然收到此错误
~~ sudo npm uninstall -g angular-cli
~~ sudo npm uninstall -g @angular/cli
~~ sudo npm cache clean --force
~~ sudo npm install -g @angular/cli
这输出:
/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
但是,发出以下命令不起作用:
~~ ng version
-bash: ng: command not found
有些人建议链接,所以我也尝试过:
~~ sudo npm link @angular/cli
,输出如下:
/Users/dinob/node_modules/@angular/cli -> /usr/local/Cellar/node/11.10.0/lib/node_modules/@angular/cli
,但 ng 版本仍然无法正常工作:
~~ ng version
-bash: ng: command not found
许多帖子建议应该在我的 /Users/dinob 目录下创建一个目录.npm-global
但我没有看到它。 我只看到.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
上面,没有 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/
此位置相同> @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/
经过几天的谷歌搜索,在这里和@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"
在此之后,确保您重新启动终端,您应该能够发出nvm --version
以查看已安装 nvm 的版本。
2.使用nvm安装节点
nvm install stable
nvm install node
3、最后安装angular
npm install -g @angular/cli
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
我现在可以创建并启动我的项目
ng new my-test-project
ng serve my-test project
我认为 SO 应该开始认真对待人们在阅读问题之前对问题投反对票或将其标记为重复问题,甚至尝试了解所问的问题和问题所在。
似乎很多人都阅读了标题并根据此决定问题的命运,任何分歧最终都会完全阻止问题。
在一个本应有所帮助的网站上存在如此多的偏见和仇恨。
非常需要更好的替代品。
更新
如果你像我一样改用 zsh shel 而不是 bash shell(因为 Catalina MacOs 现在使用 zsh),你可能已经注意到ng version
已经停止为你工作了。
在这种情况下,通过在 vim 中打开它来修改 your.zshrc 文件:
vi ~/.zshrc
在那里,找到这一行:
source $ZSH/oh-my-zsh.sh
在此行下方,添加以下行:
source /Users/Your-User-Name/.bash_profile
按 Esc 键并键入:wq
并按 Enter 保存文件
重启你的终端
重新ng version
,您应该在 zsh shell 中看到它:
如果以上所有方法都不起作用,请使用 Homebrew 安装angular-cli
。
在您的终端(ZSH)中运行它:
brew install angular-cli
喝杯咖啡,回来并运行它来测试。
ng --version
答对了。
最好的解决方案是
alias ng="/Users/YourName/.npm-global/bin/ng"
可能不会创建“ng”的环境变量。 我们可以使用以下步骤手动创建它:
通过打开您的 bash 配置文件检查是否存在“ng”别名
触摸 ~/.bash_profile; 打开 ~/.bash_profile
如果没有找到“ng”的别名,在 bash 文件中添加以下别名并保存
别名 ng=/usr/local/Cellar/node/13.5.0/lib/node_modules/@angular/cli/bin/ng
注意:@angular/cli 安装在我的以下路径 /usr/local/Cellar/node/13.5.0/lib/node_modules/@angular/cli。 因此,我将 angular cli 下的 ng 添加到别名中。
现在,使用检查 ng 版本
ng --version
这对我有用。
@像素谢谢!
有同样的问题。 我最近将终端切换到 zsh 并在 Visual Studio Code 中也这样做了,但从未完全关闭所有正在运行的 windows。 好吧,我昨晚终于退出了 Visual Studio Code,今天ng serve
命令不起作用。 我注意到我的 Visual Studio 的 shell 环境现在设置为 zsh ...
2 分钟后按照您的步骤进行操作,我重新启动并运行。
我遇到了同样的问题,但解决方案是添加到我的$PATH
中。 在带有 zsh 的 MacOS Mojave 上,@Angular/CLI 没有被添加到$PATH
中。 您可以通过附加到/private/etc/paths
将其添加到您的路径中:
sudo vim /private/etc/paths
/Users/my_user_name/node_modules/@angular/cli/bin
或安装@Angular/CLI 的任何位置。:wq
)并重新启动 shell。 ng version
然后工作。
我的终端显示 zsh: command not found: ng,in visconde terminal not found command ng 而不是 Mac 终端,我看到在 bashrc 中有一行导出 ng 命令,所以我复制了 bash 内的导出和将其粘贴到 ~/.zshrc 中,现在可以使用了!
这对我有用,可以解决此错误
~ ng --version
zsh: command not found: ng
使固定:
export PATH="$HOME/.npm-global/bin:$PATH"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.