Angular爬坑記 —— @angular/cli@1.0.1依賴包版本鎖定

最近接手一個Angular項目的維護,包依賴以下:javascript

// package.json
  ···
  "dependencies": {
    "@angular/animations": "4.1.3",
    "@angular/common": "4.1.0",
    "@angular/compiler": "4.1.0",
    "@angular/core": "4.1.0",
    "@angular/forms": "4.1.0",
    "@angular/http": "4.1.0",
    "@angular/platform-browser": "4.1.0",
    "@angular/platform-browser-dynamic": "4.1.0",
    "@angular/platform-server": "4.1.0",
    "@angular/router": "4.1.0",
    "@ng-bootstrap/ng-bootstrap": "1.0.0-alpha.25",
    "@ngx-translate/core": "6.0.1",
    "@ngx-translate/http-loader": "0.0.3",
    "@ntesmail/shark-angular2": "^1.2.2",
    "aircraftcarrier-ngx-charts": "^6.0.5",
    "aircraftcarrier-oauth-access-token": "^1.1.6",
    "amcharts3": "^3.21.0",
    "ammap3": "^3.21.14",
    "angular2-cookie": "^1.2.6",
    "angular2-datatable": "0.6.0",
    "animate.css": "3.5.2",
    "body-parser": "~1.13.2",
    "bootstrap": "4.0.0-alpha.6",
    "chart.js": "1.1.1",
    "chartist": "0.10.1",
    "chroma-js": "1.3.3",
    "ckeditor": "4.6.2",
    "cookie-parser": "~1.3.5",
    "core-js": "2.4.1",
    "cross-env": "^3.2.4",
    "d3": "^4.12.2",
    "easy-pie-chart": "2.1.7",
    "egg": "^1.2.0",
    "egg-cookies": "^2.2.1",
    "egg-cors": "^1.1.0",
    "egg-development-proxyagent": "^1.0.0",
    "egg-dubbo": "^1.0.1",
    "egg-mongo-native": "^1.0.6",
    "egg-mongoose": "^2.1.0",
    "egg-multipart": "^1.3.0",
    "egg-oss": "^1.0.2",
    "egg-redis": "^1.0.2",
    "egg-scripts": "^2.11.0",
    "egg-static": "^1.3.0",
    "egg-view-nunjucks": "^2.0.0",
    "excel-export": "^0.5.1",
    "font-awesome": "4.7.0",
    "formstream": "^1.1.0",
    "fullcalendar": "3.3.1",
    "google-maps": "3.2.1",
    "html-pdf": "^2.2.0",
    "intl": "^1.2.5",
    "ionicons": "2.0.1",
    "jquery": "3.2.1",
    "jquery-slimscroll": "1.3.8",
    "leaflet": "0.7.7",
    "leaflet-map": "0.2.1",
    "lodash": "4.17.4",
    "md5": "^2.2.1",
    "moment": "^2.18.1",
    "morgan": "~1.6.1",
    "ng2-ckeditor": "1.1.6",
    "ng2-completer": "1.3.1",
    "ng2-handsontable": "0.48.0",
    "ng2-interceptors": "^1.3.0-1",
    "ng2-slim-loading-bar": "4.0.0",
    "ng2-smart-table": "1.0.3",
    "ng2-tree": "2.0.0-alpha.5",
    "ngx-uploader": "2.2.5",
    "node-xlsx": "^0.10.0",
    "normalize.css": "6.0.0",
    "roboto-fontface": "0.7.0",
    "rxjs": "5.1.1",
    "serve-favicon": "~2.3.0",
    "stream-wormhole": "^1.0.3",
    "zone.js": "0.8.5"
  },
  "devDependencies": {
    "@angular/cli": "1.0.1",
    "@angular/compiler-cli": "4.1.0",
    "@types/fullcalendar": "2.7.40",
    "@types/jasmine": "2.5.38",
    "@types/jquery": "2.0.41",
    "@types/jquery.slimscroll": "1.3.30",
    "@types/lodash": "4.14.61",
    "@types/node": "6.0.69",
    "amcharts3": "^3.21.0",
    "autod": "^2.7.1",
    "browser-sync": "latest",
    "cheerio": "^0.22.0",
    "codelyzer": "2.0.0",
    "consolidate": "^0.14.5",
    "cross-env": "^3.2.4",
    "egg-bin": "^3.2.0",
    "egg-mock": "^3.2.0",
    "eslint": "^3.13.1",
    "eslint-config-egg": "^3.2.0",
    "express": "^4.15.2",
    "gh-pages": "0.12.0",
    "glob": "^7.0.0",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "3.2.0",
    "karma": "1.4.1",
    "karma-chrome-launcher": "2.0.0",
    "karma-cli": "1.0.1",
    "karma-coverage-istanbul-reporter": "0.2.0",
    "karma-jasmine": "1.1.0",
    "karma-jasmine-html-reporter": "0.2.2",
    "lodash": "^4.17.4",
    "ncp": "^2.0.0",
    "ng2-completer": "^1.2.2",
    "npm-run-all": "4.0.2",
    "protractor": "5.1.0",
    "reload": "^1.1.1",
    "rimraf": "2.6.1",
    "standard-changelog": "1.0.1",
    "stylelint": "7.10.1",
    "supertest": "^2.0.1",
    "supervisor": "^0.12.0",
    "ts-node": "2.0.0",
    "tslint": "4.5.0",
    "tslint-eslint-rules": "3.5.1",
    "tslint-language-service": "0.9.2",
    "typescript": "2.2.2",
    "typogr": "0.6.6",
    "underscore": "1.8.3",
    "webstorm-disable-index": "^1.2.0",
    "wintersmith": "2.2.5",
    "wintersmith-sassy": "1.2.0"
  },
  "scripts": {
    "ng": "ng",
    "rimraf": "rimraf",
    "changelog": "standard-changelog",
    "start": "cross-env EGG_SERVER_ENV=prod NODE_ENV=prod node dispatch.js",
    "dev": "cross-env EGG_SERVER_ENV=test NODE_ENV=prod node dispatch.js",
    "client": "ng serve --proxy-conf build/proxy.conf.json --host localhost --port 8088",
    "serve": "cd app && eslint . && cd .. && cross-env NODE_ENV=local http_proxy=http://127.0.0.1:8888 egg-bin dev",
    "start:hmr": "ng serve --hmr",
    "start:aot": "ng serve --aot",
    "start:prod": "ng serve --prod",
    "start:prod:aot": "ng serve --prod --aot",
    "build": "npm run clean:dist && ng build --output-path app/goodsCarriers",
    "build:prod": "npm run build -- --prod",
    "build:prod:aot": "npm run build:prod -- --aot",
    "build:ci": "npm run build:prod && npm run build:prod:aot",
    "test": "ng test -sr",
    "test:coverage": "npm run clean:coverage && ng test -sr -cc",
    "lint": "ng lint",
    "lint:styles": "stylelint ./src/app/**/*.scss",
    "lint:ci": "npm run lint && npm run lint:styles",
    "pree2e": "webdriver-manager update --standalone false --gecko false",
    "e2e": "ng e2e",
    "clean:dist": "npm run rimraf -- app/goodsCarriers",
    "clean:coverage": "npm run rimraf -- coverage",
    "docs:deploy": "wintersmith build -C docs && gh-pages -d docs/build",
    "docs:serve": "wintersmith preview -C docs"
  }
  ···
複製代碼

package.json文件中的依賴包版本鎖定與否的依據是啥,版本號^~的區別是啥?

^ 大版本;~小版本。保留這些符號即不鎖定依賴包的版本號,不然鎖定。所有去掉最保險,但內部包不要去掉。css

啓動項目

  1. 第一步:按照項目README.md文件說明運行npm install安裝項目依賴。
  2. 第二步:依賴包安裝完成後,運行npm run client命令啓動客戶端服務。報錯如圖:
    客戶端啓動報錯

一般,檢測項目環境如何都會運行相關命令查看版本號,若是有正常輸出,說明環境存在;不然環境待安裝。如: html

查看node、npm、nrm版本有對應版本號輸出,環境正常

同理,「'ng'不是內部或外部命令,也不是可運行的程序或批處理文件。」初步猜想是Angular環境未安裝。運行ng v,如圖: java

查看ng版本,未輸出對應版本號

如此,那運行npm install -g @angular/cli安裝下Angular CLI吧~如圖: node

運行npm install -g @angular/cli安裝Angular CLI

再次運行npm run client命令嘗試啓動客戶端服務。報錯如圖: jquery

客戶端服務啓動再次失敗

回頭看package.json文件中依賴包的版本設置,初步猜想是版本號不匹配報錯。那咱嚴格按照package.json文件中指定的依賴版原本安裝吧!web

Angular指定版本安裝

  1. 卸載以前的版本

    npm uninstall -g @angualr/cliredis

  2. 清理緩存,確保卸載乾淨

    npm cache verifychrome

  3. 檢測是否卸載乾淨:顯示common not found即卸載乾淨

    ng vtypescript

  4. 安裝指定Angular版本

    npm install -g @angular/cli@1.0.1

一通折騰後,運行ng v,輸出如圖:

Angular指定版本安裝

可見,好些@angular/包都未成功安裝,再次運行npm run client嘗試啓動項目,仍舊報錯,如圖:

指定angular/cli安裝後項目客戶端啓動仍舊失敗

鬱悶!npm install不是安裝全部package.json文件中所列的依賴嗎?怎麼啓動項目的時候仍是有依賴未安裝的提示呢?極有是該項目的版本太舊了...可是當下也沒太多精力對其升級,怎麼辦?

  1. 根據terminal提示信息,缺哪一個依賴再臨時補哪一個依賴 —— 依賴太多,費勁兒!
  2. 有沒一種方式完善npm install未成功安裝的依賴。分析目前的信息提示,大可能是dependencies中的依賴缺失,那單獨再統一安裝一次呢?

    百度找到:npm安裝package.json中的模塊依賴

運行npm install --dependencies完成後,再次運行npm run client,終於再也不報錯了,如圖:

客戶端啓動成功

啊!啊!啊!安全綠~成功人士頭頂都須要那麼點綠,看來是真的~哈哈哈,將來可期!將來可期...

至此,項目維護第一步 —— 項目啓動已解決,繼續搬磚ING~

小結

這次問題關鍵是@angular/cli@1.0.1依賴包版本鎖定致使的依賴包安裝不徹底引發的。確保相關依賴成功安裝後,沒有啓動不起來的項目。fighting~

相關文章
相關標籤/搜索