命令“ngserve”在命令提示符下有效,但与同一项目的代码相比不起作用

问题描述 投票:0回答:2

我是使用 Angular 或 Visual Studio 代码的新手。我安装了node.js 和npm,它们运行良好。用于运行项目的命令

ng serve
在命令提示符下完美运行。但是,当我切换到 VS Code 的终端区域并运行相同的命令
ng serve
时,它会抛出如下错误。

ng : The term 'ng' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At line:1 char:1
+ ng serve
+ ~~
    + CategoryInfo          : ObjectNotFound: (ng:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

再说一遍,如果我使用

npm run ng serve
,它的工作方式就像在 VS Code 终端中一样。 是这样的吗?如果没有,我该怎么做才能使其仅使用
ng serve
来运行项目?

node.js npm visual-studio-code angular-cli
2个回答
1
投票

我认为你可以在文件夹中按Shift +右键单击,这样你就可以打开powershell,然后执行

npm install -g @angular/cli
,然后生成你的项目。我认为应该有效。


0
投票

希望这能在未来避免有人头痛 - 我遇到了这个确切的问题,因为我经常切换终端,并且当我处理不同 Angular 版本上的开发时,我还使用 nvm 切换节点版本......我不断收到“command ng”的原因未找到',是因为您使用的每个版本的节点都有其自己的node_modules全局文件夹,因此您需要为每个节点版本全局安装Angular。我的文件夹结构是 $HOME/.nvm/versions/x.x.x/lib/node_modules/@angular

确保为与 nvm 一起使用的每个节点版本全局安装软件包!

© www.soinside.com 2019 - 2024. All rights reserved.