我刚刚开始使用Angular和 Angular-CLI ,我已经看到了,根据文档,我需要使用{{1}安装$ npm install -g @angular/cli
}(全球)旗帜。
但是我想在本地安装Angular-CLI和其他 node_modules 软件包。这样,当我从 git 下载项目时,我只需运行-g
(用于在我的 package.json 中安装所有依赖项)。
我尝试通过运行$ npm install
创建新项目,然后运行$ npm init
($ npm i @angular/cli -D
与-D
相同)。但是当我运行--save-dev
时,使用单独的 node_modules 目录创建了一个新的子目录。
答案 0 :(得分:17)
正如一些评论所示,您可以在系统上使用本地和全球版本的角度cli。
为了能够访问您的本地版本而不是全局版本(假设您在本地安装了不同的版本,那么您的全局安装)使用npm run-script ng generate component SomeCoolComponent
@ViewChild(ChildComponent) child: ChildComponent;
在github上查看这个回答类似的问题:https://github.com/angular/angular-cli/issues/5955#issuecomment-320273493
答案 1 :(得分:5)
使用名为npx npm i -g npx
的软件包,当您需要创建角度项目时,首次使用此命令npx -p @angular/cli ng new hello-world-project
所以例如,如果你想创建angular 4项目修改上面的命令,包括angular-cli版本1.4.10,就像这个npx -p @angular/cli@1.4.10 ng new hello-world-project
,然后当你的项目设置完成时,你可以返回使用正常的ng generate
和其他命令。
Angular-cli版本指示将关联哪个角度版本 有一个项目& angular-cli 1.4.10创建了角度4个项目
答案 2 :(得分:1)
如何在系统上安装较低版本的angular或其他版本的angular 确保已全局安装更新的版本 npm安装g @ angular / cli
转到项目目录,该目录具有较低版本或不同于全球安装的角度项目的其他版本 例如,如果您想在角度2上工作,请运行以下命令
npm install
ng serve
Note: don't copy any of your node modules from your previous project if the current project you want to work on ,its version its different from formal project which you work on in the past
check
"@angular/cli": "version"
check "@angular/cli": "version", which is located in package.json
if its version 1.2.0 its angular 2,
version 1.7.0 its angular 6
答案 3 :(得分:1)
在使用命令进行cli的gobal安装之后
npm i -g @angular/cli@(latest or 1)
ng new app-name
转到要在其中创建角度应用程序的文件目录。
使用命令ng new any-name
创建角度应用,全局角度cli将负责安装本地cli版本。
要为新项目手动安装,
npm init -y
npm install @angular/cli@(latest or 1)
(在使用下一个命令之前删除package.json)
ng new app-name
此处 ng 将使用本地cli版本基于已安装的本地ng版本创建angular应用版本5或6或7。
在现有的本地安装中仅使用命令
npm install @angular/cli@(latest or 1)
。
答案 4 :(得分:1)
要在本地安装angular:
npm init -y
npm i @angular/cli
npx ng new app-name
要更新本地安装的角度版本,例如将8.x升级到9.x,可以使用
npx ng update @angular/core@9 @angular/cli@9
答案 5 :(得分:0)
npm生态系统已经越来越多地朝着作为本地项目devDependencies
安装工具的方向发展,而不是要求用户在全球范围内进行安装。这被认为是一种好习惯。
如果您将cli安装为devDependency
,换句话说,您在package.json
中包含以下几行:
devDependencies": {
"@angular/cli": "X.X.X",
您只需在项目内部运行npx
。
npx ng version
这将在node_modules/.bin/
文件夹内搜索ng
链接,该链接实际上指向../@angular/cli/bin/ng
答案 6 :(得分:0)
只需遵循此命令
npm install @angular/cli
对我有用。
答案 7 :(得分:0)
要在本地安装angular,请按照以下步骤操作- 假设角度8是全局安装的,我们需要在本地安装角度6- 我们将在C驱动器中创建一个名为“ angular6”的文件夹,并在其中创建一个名为“ ng6-test-project”的angular 6项目。
在终端中键入以下命令-
c:\> md angular6
c:\> cd angular6
c:\angular6> md ng6-test-project
c:\angular6> cd ng6-test-project
c:\angular6\ng6-test-project> npm install @angular/cli@6.1.1
c:\angular6\ng6-test-project> cd..
c:\angular6> npx -p @angular/cli@6.1.1 ng new ng6-test-project
c:\angular6> cd ng6-test-project
c:\angular6\ng6-test-project> npx ng --version
c:\angular6\ng6-test-project> npm audit fix
c:\angular6\ng6-test-project> npx ng serve --port 4201
如果任何软件包和版本(例如rxjx等)中发生任何错误 打开package.json并找到“ rxjs”:“〜6.4.0”并将其更改为“ rxjs”:“ 6.0.0”并保存。 然后在终端中,键入以下内容以更新rxjs-
c:\angular6\ng6-test-project> npm install
要修复软件包中的任何警告,请运行以下命令-
c:\angular6\ng6-test-project> npm audit fix
键入以下内容以在本地检查角度版本
c:\angular6\ng6-test-project> npx ng --version
这将导致新的角度版本保存在本地
要在新端口中运行项目,请输入以下内容-
c:\angular6\ng6-test-project> npx ng serve --port 4201
答案 8 :(得分:-1)
要从命令行使用ng,它必须位于该目录或系统PATH变量中。
在您有项目之前,您没有可用的项目。使用npm i @angular/cli
安装它只会将其安装在node_modules文件夹中,而不是在PATH中可用。
因此,它需要至少全局安装一次,因为全局安装的节点模块可以选择在PATH中使用。
完成此操作后,您可以在项目文件夹中安装您选择的版本,因为该版本将在全球版本存在时使用。