简体   繁体   English

ng:使用angular-cli创建新项目时找不到命令

[英]ng: command not found while creating new project using angular-cli

Installed angular-cli globally using ( npm install -g angular-cli ) but when I'm trying to create project using ng new my-project it is throwing error:使用( npm install -g angular-cli )全局安装了angular-cli但是当我尝试使用ng new my-project创建项目时它抛出错误:

ng: command not found ng:找不到命令

The issue is simple, npm doesn't know about ng问题很简单, npm不知道ng

Just run npm link @angular/cli and it should work seamlessly.只需运行npm link @angular/cli ,它应该可以无缝工作。

First, angular-cli is deprecated and has been replaced with @angular/cli .首先, angular-cli已被弃用并已替换为@angular/cli So if you uninstall your existing angular-cli with npm uninstall angular-cli , then reinstall the package with the new name @angular/cli you might get some conflicts.因此,如果您使用npm uninstall angular-cli卸载现有的 angular-cli,然后使用新名称 @angular/cli 重新安装软件包,您可能会遇到一些冲突。 My story on Windows 7 is:我在 Windows 7 上的故事是:

I had installed angular-cli and reinstalled using npm install -g @angular/cli , but after doing some config changes to command-line tools, I started getting the ng command not found issue.我已经安装了 angular-cli 并使用npm install -g @angular/cli重新npm install -g @angular/cli ,但是在对命令行工具进行一些配置更改后,我开始遇到 ng 命令未找到问题。 I spent several hours trying to fix this but none of the above issues alone worked.我花了几个小时试图解决这个问题,但上述问题都没有单独奏效。 I was able to fix it using these steps:我能够使用以下步骤修复它:

Install Rapid Environment Editor and remove any PATH entries for node, npm, angular-cli or @angular/cli.安装Rapid Environment Editor并删除 node、npm、angular-cli 或 @angular/cli 的所有 PATH 条目。 Node.js will be in your System path, npm and angular entries are in the User path. Node.js 将在您的 System 路径中,npm 和 angular 条目在 User 路径中。

Uninstall node.js and reinstall the current version (for me 6.11.1).卸载 node.js 并重新安装当前版本(对我来说是 6.11.1)。 Run Rapid Environment Editor again and make sure node.js and npm are in your System or User path.再次运行 Rapid Environment Editor 并确保 node.js 和 npm 在您的系统或用户路径中。 Uninstall any existing ng versions with:卸载任何现有的 ng 版本:

npm uninstall -g angular-cli

npm uninstall -g @angular/cli

npm cache clean

Delete the C:\\Users\\%YOU%\\AppData\\Roaming\\npm\\node_modules\\@angular folder.删除C:\\Users\\%YOU%\\AppData\\Roaming\\npm\\node_modules\\@angular文件夹。

Reboot, then, finally, run:重新启动,然后,最后,运行:

npm install -g @angular/cli

Then hold your breath and run:然后屏住呼吸,跑:

ng -v

If you're lucky, you'll get some love.如果你幸运,你会得到一些爱。 Hold your breath henceforward every time you run the ng command, because 'command not found' has magically reappeared for me several times after ng was running fine and I thought the problem was solved.以后每次运行 ng 命令时都要屏住呼吸,因为在 ng 运行良好后,“找不到命令”神奇地再次出现在我面前,我认为问题已解决。

Make sure that the npm directory is in your " Path " variable.确保 npm 目录在您的“ Path ”变量中。

If the module is installed properly, it may work if you start it out of your global node module directory, but your command line tool doesn't know where to find the ng command when you are not in this directory.如果模块安装正确,如果您从全局节点模块目录中启动它可能会工作,但是当您不在此目录中时,您的命令行工具不知道在哪里可以找到ng命令。

For Win system variable add something like:对于Win系统变量添加如下内容:

%USERPROFILE%\AppData\Roaming\npm

And if you use a unix-like terminal (emulator):如果您使用类Unix 终端(模拟器):

PATH=$PATH:[path_to_your_user_profile]/path-to-npm

the easiest solution is ( If you have already installed angular ) :最简单的解决方案是(如果您已经安装了 angular ):

1 remove the ng alias if existing 1 删除 ng 别名(如果存在)

unalias ng

2 add the correct alias 2 添加正确的别名

alias ng="/Users/<user_name>/.npm-global/bin/ng"

3 run ng serve for example and it will work. 3 运行 ng serve 例如,它会工作。

This is how I made it worked for me :).这就是我让它为我工作的方式:)。

1 - npm link @angular/cli 1 - npm link @angular/cli

It will return you the path of cli , which will look like this它会返回给你cli的路径,它看起来像这样

/usr/local/Cellar/node/11.3.0_1/lib/node_modules/@angular/cli

For this part, /11.3.0_1 please replace this with your respective node version that can be found by typing node --version对于这部分,/ /11.3.0_1请将其替换为您可以通过键入node --version找到的相应节点版本

2 - cd ~/ 2 - cd ~/

3 - open .bash_profile 3 - open .bash_profile

In the bash profile create an alias for cli like this,在 bash 配置文件中,像这样为cli创建一个别名

alias ng="/usr/local/Cellar/node/11.3.0_1/lib/node_modules/@angular/cli/bin/ng"

4 - source ~/.bash_profile 4 - source ~/.bash_profile

This is how your .bash_profile will look like once you add alias to it.这是.bash_profile添加别名后的样子。

在此处输入图片说明

Now typing ng in the terminal will display output shown in attached snapshot.现在在终端中输入ng将显示附加快照中显示的输出。

在此处输入图片说明

I hope this answer will be helpful.我希望这个答案会有所帮助。

For Mac run对于 Mac 运行

npm install -g @angular/cli@latest

then run然后运行

alias ng="/usr/local/lib/node_modules/node/lib/node_modules/@angular/cli/bin/ng"

Check if working检查是否工作

ng version

Run below commands:运行以下命令:

npm uninstall -g angular-cli

npm uninstall -g @angular/cli

npm cache clean

npm install -g @angular/cli@latest

alias ng="C:/Users/itaas/.npm-global/ng" ( Location of ng file in npm folder) alias ng="C:/Users/itaas/.npm-global/ng" (npm 文件夹中 ng 文件的位置)

And finally run :最后运行:

ng -v 

吴-v

我已经通过添加别名解决了同样的问题,例如:

alias ng="path-to-your-global-node-modules/angular-cli/bin/ng"

In my case install angular cli在我的情况下安装 angular cli

npm install -g @angular/cli@latest

update Nodejs to latest , then all should work fine.将 Nodejs 更新为 latest ,然后一切正常。

...and if you still have that problem, it maybe because you run the command in shell and not in cmd (you need to run command in cmd), check this out and maybe it helps... ...如果你仍然有这个问题,那可能是因为你在 shell 中而不是在 cmd 中运行命令(你需要在 cmd 中运行命令),检查一下,也许它会有所帮助......

If you have installed angular cli globally but ng isn't working, just do this:如果您已全局安装 angular cli 但ng不起作用,请执行以下操作:

echo -e "export PATH=$(npm prefix -g)/bin:$PATH" >> ~/.bashrc

source ~/.bashrc

ng --version

Adding %AppData%\\npm to the Win path have worked for me.%AppData%\\npm添加到 Win 路径对我%AppData%\\npm

Source : https://github.com/angular/angular-cli/issues/1183 , the first comment.来源: https : //github.com/angular/angular-cli/issues/1183 ,第一条评论。

First of all, check if your npm and node installed properly首先,检查你的 npm 和 node 是否安装正确
with commands npm version and node -v .使用命令npm versionnode -v

If they are proper:如果它们是正确的:

  1. Find the root global Directory of NPM npm root -g (it will give you root of your global npm store)找到 NPM npm root -g的根全局目录(它将为您提供全局 npm 存储的根目录)

  2. Uninstall old angular cli with npm uninstall -g angular-cli and npm cache clean使用npm uninstall -g angular-clinpm cache clean卸载旧的 angular cli

  3. Reinstall new Version of angular npm install -g @angular/cli@latest重新安装新版本的 angular npm install -g @angular/cli@latest

  4. make an Alias of Name ng:做一个 Name ng 的别名:

alias ng="C:/ProgramData/npm/node_modules/@angular/cli/bin/ng"

alias ng="<ath-to-your-global-node-modules>/<angular cli path till ng>"
(from answered Oct 20 '16 at 15:30 @m.zemlyanoi ) (来自 16 年 10 月 20 日 15:30 @m.zemlyanoi 的回答)

then to check you can type ng -v然后检查你可以输入 ng -v

For Mac users:对于 Mac 用户:

1) Install @angular/cli globally 1)全局安装@angular/cli

$ sudo npm install -g @angular/cli
/usr/local/Cellar/node/10.0.0/bin/ng -> /usr/local/Cellar/node/10.0.0/lib/node_modules/@angular/cli/bin/ng

2) Create alias for ng 2)为ng创建别名

alias ng="/usr/local/Cellar/node/10.0.0/lib/node_modules/@angular/cli/bin/ng"

Done完毕

try this :尝试这个 :

alias ng="~/node_modules/@angular/cli/bin/ng"别名 ng="~/node_modules/@angular/cli/bin/ng"

Same problem here running Windows 10 x64 / NodeJS 6.9.1 / npm 3.10.9 .同样的问题在这里运行Windows 10 x64 / NodeJS 6.9.1 / npm 3.10.9
After installation of Angular CLI via npm:通过 npm 安装 Angular CLI 后:

'ng' command cannot be found找不到“ng”命令

Do the following:请执行下列操作:

  1. Uninstalled npm with npm uninstall -g npm使用npm uninstall -g npm
  2. Uninstalled NodeJS via control panel / programs and features通过控制面板/程序和功能卸载 NodeJS
  3. Downloaded and ran 'old' NodeJS installer release 6.5.0 from https://nodejs.org/download/release/v6.5.0/ ( node-v6.5.0-x64.msi ).https://nodejs.org/download/release/v6.5.0/ ( node-v6.5.0-x64.msi ) 下载并运行“旧” NodeJS 安装程序版本 6.5.0
  4. After NodeJS 6.5.0 installation completed, open powershell and npm install -g angular-cli NodeJS 6.5.0安装完成后,打开powershell, npm install -g angular-cli
  5. Grab quick coffee来杯速溶咖啡
  6. After installation completed, ng worked OK.安装完成后,ng 工作正常。

HTH HTH

Make sure angular-cli is installed before trying to create a project.在尝试创建项目之前,请确保安装了angular-cli Windows users can install angular-cli without giving permission to command but MAC users have to use sudo before executing the command as follow: Windows 用户可以在不授予命令权限的情况下安装angular-cli ,但 MAC 用户在执行命令之前必须使用sudo ,如下所示:

sudo npm install -g angular-cli

Type a password when asked and press enter to proceed.在询问时输入密码,然后按 Enter 继续。

For mackBook OS对于 macBook 操作系统

sudo npm uninstall -g @angular/cli
sudo npm cache verify
sudo npm install -g @angular/cli@latest

try with ng -v if not work then set alias and try尝试使用 ng -v 如果不起作用然后设置别名并尝试

alias ng="/usr/local/Cellar/node/10.0.0/lib/node_modules/@angular/cli/bin/ng"

Do you install the node js package?你安装node js包了吗? https://nodejs.org/en/ Regards https://nodejs.org/en/问候

if you find this error when you are installing angular-cli, -bash: ng: command not found try this it works,如果您在安装 angular-cli 时发现此错误,请尝试-bash: ng: command not found试试看它是否有效,

After removing Node from your system从系统中删除节点后

install NVM from here https://github.com/creationix/nvm
Install Node via NVM: nvm install stable
run npm install -g angular-cli

根据npmangular-cli已重命名为@angular/cli您可以使用以下语法来安装它。

npm install -g @angular/cli

If you are using asdf-vm you need run adsf reshim nodejs to recreate links with global modules.如果您使用asdf-vm ,则需要运行adsf reshim nodejs以重新创建与全局模块的链接。

In my case this resolve my problem, I hope resolve to you all too.在我的情况下,这解决了我的问题,我希望你们也能解决。

soluton for windows operating system only....... first step:仅适用于 Windows 操作系统的解决方案......第一步:

install nodejs version: nodev 8.1.2安装 nodejs 版本:nodev 8.1.2

second step: set up environment variable like: C:\\ProgramFiles\\nodejs第二步:设置环境变量如:C:\\ProgramFiles\\nodejs

Third step: install angular use this command: npm install -g @angular/cli第三步:安装angular 使用这个命令:npm install -g @angular/cli

after installation whereever you have to create project like: ng new first-project......安装后无论你在哪里创建项目,如:ng new first-project......

For me (on MacOSX) I had to do:对我来说(在 MacOSX 上)我必须这样做:

nvm install stable
npm install -g angular-cli

This installed ng into:这将 ng 安装到:

/usr/local/lib/node_modules/@angular/cli/bin/ng

But npm did not put a link to ng into但是npm没有把ng的链接放进去

/usr/local/bin/

Which was why it was not part of the %PATH and therefore available from the command line except via an absolute address.这就是为什么它不是 %PATH 的一部分,因此除了通过绝对地址之外,还可以从命令行获得。

So I used the following the create a link to ng:所以我使用了以下内容来创建一个指向 ng 的链接:

sudo ln -sf /usr/local/lib/node_modules/\@angular/cli/bin/ng /usr/local/bin/ng

running跑步

export PATH=$PATH:/c/Users/myusername/AppData/Roaming/npm 

helped.有帮助。

Make sure your actual username is in the myusername section确保您的实际用户名在 myusername 部分

在 Windows 上修复 NodeJS 安装为我解决了这个问题。

I came here because I had the same issue on windows.我来这里是因为我在 Windows 上遇到了同样的问题。 The thing that baffled me most on this was I have had Node installed since Angular 4 dabbling here and there.最让我困惑的是,自从 Angular 4 到处涉足以来,我已经安装了 Node。 And sure enough.果然。 That was my problem.那是我的问题。 I needed to upgrade node.我需要升级节点。

Just installing the newest version worked fine for me without having to do any of the above steps post the new install只需安装最新版本对我来说效果很好,而无需在新安装后执行上述任何步骤

If you are working in windows 7 and you can not run command start with ng如果您在Windows 7中工作并且无法运行命令 start with ng

please, update the angular/CLI at once and try to use ng commands请立即更新 angular/CLI 并尝试使用 ng 命令

use below comman to update latest CLI使用以下命令更新最新的 CLI

npm install -g @angular/cli@latest

This works to update your angular/cli //*Global package (cmd as administrator)这适用于更新您的 angular/cli //*Global 包(cmd 作为管理员)

npm uninstall -g @angular/cli
npm cache verify
npm install -g @angular/cli@latest

Firstly install '@angular/cli' by using npm as global module首先使用 npm 作为全局模块安装“@angular/cli”

sudo npm install -g @angular/cli

After that try to ping the cli by using 'ng' command from the terminal window.If any problem like之后尝试使用终端窗口中的“ng”命令ping cli。如果有任何问题,如

Command 'ng' not found未找到命令“ng”

Then you need to manually setup the然后你需要手动设置

ng ng

command availability.You set this step by running the following command.命令可用性。您可以通过运行以下命令来设置此步骤。

ln -s path-to-your-ng-command /bin

eg:例如:

sudo ln -s /opt/node/lib/node_modules/@angular/cli/bin/ng /bin/须藤 ln -s /opt/node/lib/node_modules/@angular/cli/bin/ng /bin/

In my case, it was happening when I restarted working on an adjacent tab.就我而言,它发生在我重新开始处理相邻选项卡时。 It was the npm which was not set.这是未设置的 npm。 Please check the version of npm wither using请使用检查 npm wither 的版本

npm -v npm -v

or if you are having nvm then或者如果你有 nvm 那么

nvm list虚拟机列表

or或者

nvm current非易失性电流

I set it to 8.9.1 and then it started working again.我将它设置为 8.9.1,然后它又开始工作了。 I hope it helps.我希望它有帮助。

Installed angular-cli globally using ( npm install -g angular-cli ) but when I'm trying to create project using ng new my-project it is throwing error:使用( npm install -g angular-cli )全局npm install -g angular-cli但是当我尝试使用ng new my-project创建ng new my-project它抛出错误:

ng: command not found ng:找不到命令

Installed angular-cli globally using ( npm install -g angular-cli ) but when I'm trying to create project using ng new my-project it is throwing error:使用( npm install -g angular-cli )全局npm install -g angular-cli但是当我尝试使用ng new my-project创建ng new my-project它抛出错误:

ng: command not found ng:找不到命令

Installed angular-cli globally using ( npm install -g angular-cli ) but when I'm trying to create project using ng new my-project it is throwing error:使用( npm install -g angular-cli )全局npm install -g angular-cli但是当我尝试使用ng new my-project创建ng new my-project ,抛出错误:

ng: command not found ng:找不到命令

如果已安装zsh,则也应在主目录的.zshrc文件中添加别名。

Maximum time, this is a path issue conflict between npm and angular cli.最长时间,这是 npm 和 angular cli 之间的路径问题冲突。

The solution that worked for me ->对我有用的解决方案->

  1. %USERPROFILE%\\AppData\\Roaming Paste this in File Explorer and delete npm folder. %USERPROFILE%\\AppData\\Roaming将此粘贴到文件资源管理器中并删除npm文件夹。
  2. npm install -g @angular/cli@latest run this command to reinstall latest angular cli. npm install -g @angular/cli@latest运行此命令以重新安装最新的 angular cli。
  3. npm link @angular/cli now this command can solve your path issue npm link @angular/cli现在这个命令可以解决你的路径问题

Literally that three steps will fix your issue, otherwise you need to update environment variable by search and open Edit the system environment variables从字面上看,三个步骤将解决您的问题,否则您需要通过搜索和打开来更新环境变量编辑系统环境变量

Ariful Ahsan https://arifulahsan.com Ariful Ahsan https://arifulahsan.com

If you use enterprise computer and unable to access system environments如果您使用企业计算机并且无法访问系统环境

use RapidEE to edit user variables then add to path these使用RapidEE编辑用户变量然后添加到路径这些

%AppData%\npm

or或者

%USERPROFILE%\AppData\Roaming\npm

it worked for me on windows它在 Windows 上对我有用

Most likely the problem is that you are using PowerShell and not the command line.最有可能的问题是您使用的是 PowerShell 而不是命令行。 If you use Windows PowerShell run:如果您使用 Windows PowerShell 运行:

cmd

then next try:然后下一次尝试:

ng --version

if it works run:如果它工作运行:

ng new my-project

I was facing same issue, I was using git bash terminal, I installed angular cli using this command我遇到了同样的问题,我使用的是 git bash 终端,我使用这个命令安装了 angular cli

npm install -g @angular/cli

but when I try to create new app it was giving me error ng: command not found, I just restarted my pc and it worked, even closing and opening terminal again will also fix issue sometimes但是当我尝试创建新应用程序时,它给了我错误 ng: command not found,我刚刚重新启动了我的电脑并且它工作了,即使再次关闭和打开终端也会有时解决问题

Permanently solution here for MACOS Users: MACOS用户的永久解决方案:

open terminal:打开终端:

sudo vim /etc/Paths

add new line.添加新行。 (INSERT , press fn+i) (插入,按 fn+i)

/Users/[your-user]/.npm-global/bin

press ESC , and then :wq (exit from editor)按 ESC ,然后按 :wq (从编辑器退出)

restart terminal (close and open terminal again..) check the path;重启终端(关闭并再次打开终端..)检查路径;

~ % echo $PATH

it should be listed in path list now.. like below;它现在应该列在路径列表中。如下所示;

/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/[myuser]/.npm-global/bin ....

good job, ng command now,干得好,现在 ng 命令,

ng version版本

Angular CLI: 14.1.0

After trying everything here, what solved my issue was:在这里尝试了一切之后,解决我的问题的是:

  1. Making sure I have the latest node.js version (re-install from here: https://nodejs.org/en/download/ )确保我有最新的node.js版本(从这里重新安装: https ://nodejs.org/en/download/)
  2. Running the commands from the powershellpowershell运行命令

for me, on Linux:对我来说,在 Linux 上:

you can open the.bashrc file from Home then, you will notice the following command right there:你可以从主页打开 .bashrc 文件,然后你会注意到下面的命令:

# Load Angular CLI autocompletion.
source <(ng completion script)

comment out the "source" command that you see above by # sign then, save the file.用 # 符号注释掉上面看到的“source”命令,然后保存文件。

# source <(ng completion script)

reopen the terminal then, you could use it now.然后重新打开终端,你现在可以使用它了。

you can now use the following command instead of ng:您现在可以使用以下命令代替 ng:

open the working directory where the Angular project is there then, try this command instead:然后打开 Angular 项目所在的工作目录,试试这个命令:

$ npm start

you can open the package.json file and move to the line where the "scripts" key is, move to "start" key then, you will notice the ng serve that you were like to type then, you can add host and port from your choice like so:你可以打开 package.json 文件并移动到“scripts”键所在的行,然后移动到“start”键,你会注意到你想输入的ng serve然后,你可以从添加主机和端口你的选择是这样的:

"start": "ng serve --host 0.0.0.0 --port 4200" “开始”:“ng serve --host 0.0.0.0 --port 4200”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM