Angular4中 “@angular/compiler-cli” 报错的解决方案

本人初学Angular,跟着官网教程时通过ng new my-app创建项目后,在ng serve --open时,会报如下错误:
The "@angular/compiler-cli" package was not properly installed. Error: Error: Cannot find module '@angular/compiler'

The "@angular/compiler-cli" package was not properly installed. Error: Error: Cannot find module '@angular/compiler'
Error: The "@angular/compiler-cli" package was not properly installed. Error: Error: Cannot find module '@angular/compiler'
    at Object.<anonymous> (D:\nodejs\node_modules\@angular\cli\node_modules\_@ngtools_webpack@1.7.4@@ngtools\webpack\src\index.js:14:11)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\nodejs\node_modules\@angular\cli\tasks\eject.js:10:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)

在其它地方搜索了多种解决方案,但不理想。而后在stackoverflow看到的一个可能是外国朋友的帖子:
https://stackoverflow.com/que...
就说很可能是angular没有正确安装,然后结合再看了Angular CLI 的github:
https://github.com/angular/an...
果然把这个困扰我大半天的烦人问题给解决了,现分享解决方案如下:
1.安装好node和npm,这个自不必多说,我这里指定了npm为淘宝的cnpm,原因你懂的,网速问题......
2.如果你的Angular CLI 是1.0.0-beta.28或者更早的版本,必须卸载没商量,命令如下:

cnpm uninstall -g angular-cli
cnpm uninstall --save-dev angular-cli

3.你必须把全局和本地项目的Angular CLI更新到一个新版本:
在全局端:

cnpm uninstall -g @angular/cli
cnpm cache clean
*(或者:cnpm cache verify ,当npm版本>5时)*
cnpm install -g @angular/cli@latest

在你的本地项目端:

rm -rf node_modules dist 
* rmdir /S/Q node_modules dist (在Windows 命令行下)*
* rm -r -fo node_modules,dist (在Windows PowerShell下)*
cnpm install --save-dev @angular/cli@latest
cnpm install

4.此时进入项目目录,输入命令
ng serve --open
应该就没问题了。
呼。。。。。。就是这样的报错,搞的我弄了大半天,现在终于可以再次愉快地撸代码了。。。


PS:另外,为保险起见,我多安装了两个东西

cnpm install -g @angular/compiler-cli
cnpm install webpack -g

这是在问题没解决前出现的缺少这两个插件的情况,补上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值