angular2 cli 无法正确安装使用解决

  最近开始学习使用ng2在项目里面, 然后按照指南一步一步做, 在安装 ng cli 的时候各种报错, 折腾一上午之后, 终于顺利运行。

  后面总结发现其实都是在浪费时间。安装的整个过程, 基本是遇到一个报错就去google然后解决, 解决完了就是一个新的报错, 痛苦不堪。

 

  下面大致的记录一些报错和解决方案( 其实都是在浪费时间, 解决问题请直接看下面的总结 ):

  error TS2304: Cannot find name '_'

  http://stackoverflow.com/questions/34660265/importing-lodash-into-angular2-typescript-application

  解决办法:

   Here is how to do this as of Typescript 2.0: (tsd and typings are being deprecated in favor of the following):

$ npm install --save lodash

# This is the new bit here: 
$ npm install --save @types/lodash

  Then, in your .ts file:

  Either:

import * as _ from "lodash";

    Or (as suggested by @Naitik):

import _ from "lodash";

 

  报错:  

  pm\node_modules@angular\cli\models\config\config.js:15
  constructor(_configPath, schema, configJson, fallbacks = []) {
  ^

  SyntaxError: Unexpected token =
  at exports.runInThisContext (vm.js:53:16)
  at Module._compile (module.js:373:25)
  at Object.Module._extensions..js (module.js:416:10)
  at Module.load (module.js:343:32)
  at Function.Module._load (module.js:300:12)
  at Module.require (module.js:353:17)

  https://github.com/angular/angular-cli/issues/4892

  解决办法: nodejs 版本不够Angular CLI requires node 6.9 or greater. 注意更新版本, 更新办法很多,

               但是请注意如果网络不是很好, 可能通过一些远程工具更新的时候会出错, 所以一个比较稳当的办法是,

               直接从nodejs 的官网下安装包下来更新! 中间用了一些工具来更新, 结果更新出来貌似有点问题,

               最后还是从官网下的安装包, 就OK了。。。

 

  中间还有各种一堆报错:

  angularCLI Webpack: ERROR in TypeError: Cannot read property 'request' of undefined

  TypeError: Cannot read property 'config' of null

  。。。。一大堆

 

  总结:

    不得不说现在整个前端的发展速度确实是蓬勃向上, 各种工具的版本更新速度是非常快的, nodejs, webpack, ngcli,

    所以在使用这些的工具的时候, 最好确认当前版本都是最新的版本, 这次浪费那么多时间, 实际上就是因为自己机器上的nidejs, webpack的版本都非常落后了, 安装以后一直没更新,  导致安装新的工具ngcli的时候, ngcli 依赖这些工具的新版本, 结果陷入解决一个报错又陷入另外报错的窘境里面。

    正确做法应该直接更新基础工具的版本, 然后在找到最近的说明文档, 安装最新版本的ngcli。

    https://github.com/angular/angular-cli/issues/1922

    1. 更新nodejs npm 版本到最新, nodejs 最好去官网下安装包

        https://nodejs.org/en/download/current/

    2. 保证你的typescript( 我想大部分ng2 开发还是用的ts吧? ) 版本最近 2.0+

        npm install typescript@next will update you to 2.1.0

    3. ngcli 版本更新很多, 各种配置依赖估计也是一直在变化, 

        有可能你的机器上安装了老版本的cli, so 先彻底卸载老版本的cli, 然后安装最新版本的

     npm uninstall -g angular-cli

     npm install --save-dev angular-cli@latest

     ng new projectname Creates your new project

     cd projectname moves you into the newly created directory 

     4. 保证依赖文件没问题, 可能各种依赖配置出现问题, 我这里办法比较粗暴, 直接利用ngcli新建了一个项目然后吧APP拷贝过去。

posted @ 2017-03-10 11:51  海狸先森  阅读(7108)  评论(0编辑  收藏  举报