angular2 cli 沒法正確安裝使用解決

  最近開始學習使用ng2在項目裏面, 而後按照指南一步一步作, 在安裝 ng cli 的時候各類報錯, 折騰一上午以後, 終於順利運行。前端

  後面總結髮現其實都是在浪費時間。安裝的整個過程, 基本是遇到一個報錯就去google而後解決, 解決完了就是一個新的報錯, 痛苦不堪。node

 

  下面大體的記錄一些報錯和解決方案( 其實都是在浪費時間, 解決問題請直接看下面的總結 ):webpack

  error TS2304: Cannot find name '_'git

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

  解決辦法:web

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

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

  Then, in your .ts file:npm

  Either:網絡

import * as _ from "lodash";

    Or (as suggested by @Naitik):angular2

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拷貝過去。

相關文章
相關標籤/搜索