简体   繁体   English

为什么我在 angular 项目中的 vscode 中的 zsh 终端中使用 ng serve 出现此错误

[英]Why I have this error using ng serve in zsh terminal in vscode inside an angular project

Reading this I found that it is possible to use zsh inside VSCode so I configured these inside settings.json for my user:阅读本文后,我发现可以在 VSCode 中使用 zsh,因此我在settings.json 中为我的用户配置了这些:

"terminal.integrated.shell.linux": "/usr/bin/zsh",
"terminal.integrated.fontFamily": "'SauceCodePro Nerd Font Mono','Source Code Pro'",
"terminal.integrated.fontSize": 14,

But when I tried to start my Angular app ng serve , it shows:但是当我尝试启动我的 Angular 应用程序ng serve ,它显示:

zsh: command not found: ng

So following this response that had a similiar issue I added this to the .zshrc file:因此,按照这个有类似问题的回复,我将其添加到.zshrc文件中:

if [[ -s '/etc/zsh_command_not_found' ]]; then
  source '/etc/zsh_command_not_found'
fi

But now it shows this, and I am not sure that it is the right package to install:但是现在它显示了这一点,我不确定它是否是要安装的正确软件包:

➜ ng serve

No se ha encontrado la orden «ng», pero se puede instalar con:

sudo apt install ng-common

What I can do?我可以做什么? Because with bash it works fine.因为使用 bash 它可以正常工作。

I found out that I was missing loading nvm in the zshrc file.我发现我没有在 zshrc 文件中加载nvm So I added this to the .zshrc file:所以我将它添加到 .zshrc 文件中:

export NVM_DIR="/home/espe/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Then I thought someone should have faced this before, and here is an article about how to solve this problem .然后我想之前应该有人遇到过这个问题,这里有一篇关于如何解决这个问题的文章 So the previous code became this:所以之前的代码变成了这样:

# lazyload nvm
# all props goes to http://broken-by.me/lazy-load-nvm/
# grabbed from reddit @ https://www.reddit.com/r/node/comments/4tg5jg/lazy_load_nvm_for_faster_shell_start/

lazynvm() {
  unset -f nvm node npm npx 2>/dev/null
  export NVM_DIR=~/.nvm
  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
  if [ -f "$NVM_DIR/bash_completion" ]; then
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
  fi
}

nvm() {
  lazynvm 
  nvm $@
}

node() {
  lazynvm
  node $@
}

npm() {
  lazynvm
  npm $@
}

npx() {
  lazynvm
  npx $@
}

# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)

for cmd in "${NODE_GLOBALS[@]}"; do
  eval "${cmd}(){ unset -f ${NODE_GLOBALS} 2>/dev/null; lazynvm; ${cmd} \$@ }"
done

Also, thanks to scttcper , for the improved solution.另外,感谢scttcper改进的解决方案。

Source:来源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 ng serve 在 angular 中会抛出错误? - Why does ng serve throw error in angular? 使用角度控制台工具时如何在终端上运行服务? - How to run ng serve on terminal when using angular console tool? 运行 `ng update @angular/cli @angular/core` 后 `ng serve` 出错,无法为 Angular 项目提供服务 - Error from `ng serve` after running `ng update @angular/cli @angular/core` and unable to serve Angular project 我正在尝试使 ng serve 但我在终端中收到此错误 - I am trying to make ng serve but I got this error in terminal 您必须在 Angular CLI 项目中才能使用 serve 命令。 运行动态 Web 项目时出错 - You have to be inside an Angular CLI project in order to use the serve command. Error on running a dynamic web project 我有一个新项目,但是在执行 ng serve 时,出现错误 - I have a new project, but when execute ng serve, I get error 当我使用“ng serve --open”运行我的 Angular 项目时,出现错误 - As i run my Angular project with “ ng serve --open ” I get an error 尝试使用 ng serve 为 angular 应用程序提供服务时出错 - Error when trying to serve angular application using ng serve 如何在不使用ng serve的情况下提供Angular 5应用程序? - How can I serve an Angular 5 app without using ng serve? Angular 2项目中的ng serve命令提示错误549 - ng serve command prompting error 549 in Angular 2 project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM