最近開始學習使用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拷貝過去。