无法将角度更新到版本 6

Posted

技术标签:

【中文标题】无法将角度更新到版本 6【英文标题】:Can't update angular to version 6 【发布时间】:2018-11-05 16:28:50 【问题描述】:

我正在尝试将 Angular 更新到最新版本。

我已经更新了全局 @angular/cli

npm install -g @angular/cli

然后,当我尝试使用 ng update @angular/cli 更新本地 @angular/cli 时,我收到此错误:

Error: Unexpected end of JSON input
Unexpected end of JSON input

这是我的 package.json


  "name": "apimap",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": 
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  ,
  "private": true,
  "dependencies": 
    "@angular/animations": "^5.2.11",
    "@angular/cdk": "^5.2.5",
    "@angular/common": "^5.2.11",
    "@angular/compiler": "^5.2.11",
    "@angular/core": "^5.2.11",
    "@angular/flex-layout": "^2.0.0-rc.1",
    "@angular/forms": "^5.2.11",
    "@angular/http": "^5.2.11",
    "@angular/material": "^5.2.5",
    "@angular/platform-browser": "^5.2.11",
    "@angular/platform-browser-dynamic": "^5.2.11",
    "@angular/router": "^5.2.11",
    "core-js": "^2.5.7",
    "hammerjs": "^2.0.8",
    "i": "^0.3.6",
    "npm": "^6.1.0",
    "rxjs": "^5.5.11",
    "zone.js": "^0.8.26"
  ,
  "devDependencies": 
    "@angular/cli": "^1.7.4",
    "@angular/compiler-cli": "^5.2.11",
    "@angular/language-service": "^5.2.11",
    "@types/jasmine": "^2.8.7",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.0.53",
    "codelyzer": "~4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^2.0.2",
    "karma-chrome-launcher": "~2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.3",
    "karma-jasmine": "^1.1.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.3.2",
    "ts-node": "~3.3.0",
    "tslint": "~5.8.0",
    "typescript": "2.4.2"
  

格式正确。

ng serve 时项目正在编译并运行。

我正在使用 Node 的 v9.9.0 和 npm 的 6.0.1。

Heres 的 tslint.ts 文件


  "rulesDirectory": [
    "node_modules/codelyzer"
  ],
  "rules": 
    "arrow-return-shorthand": true,
    "callable-types": true,
    "class-name": true,
    "comment-format": [
      true,
      "check-space"
    ],
    "curly": true,
    "eofline": true,
    "forin": true,
    "import-blacklist": [
      true,
      "rxjs",
      "rxjs/Rx"
    ],
    "import-spacing": true,
    "indent": [
      true,
      "spaces"
    ],
    "interface-over-type-literal": true,
    "label-position": true,
    "max-line-length": [
      true,
      140
    ],
    "member-access": false,
    "member-ordering": [
      true,
      
        "order": [
          "static-field",
          "instance-field",
          "static-method",
          "instance-method"
        ]
      
    ],
    "no-arg": true,
    "no-bitwise": true,
    "no-console": [
      true,
      "debug",
      "info",
      "time",
      "timeEnd",
      "trace"
    ],
    "no-construct": true,
    "no-debugger": true,
    "no-duplicate-super": true,
    "no-empty": false,
    "no-empty-interface": true,
    "no-eval": true,
    "no-inferrable-types": [
      true,
      "ignore-params"
    ],
    "no-misused-new": true,
    "no-non-null-assertion": true,
    "no-shadowed-variable": true,
    "no-string-literal": false,
    "no-string-throw": true,
    "no-switch-case-fall-through": true,
    "no-trailing-whitespace": true,
    "no-unnecessary-initializer": true,
    "no-unused-expression": true,
    "no-use-before-declare": true,
    "no-var-keyword": true,
    "object-literal-sort-keys": false,
    "one-line": [
      true,
      "check-open-brace",
      "check-catch",
      "check-else",
      "check-whitespace"
    ],
    "prefer-const": true,
    "quotemark": [
      true,
      "single"
    ],
    "radix": true,
    "semicolon": [
      true,
      "always"
    ],
    "triple-equals": [
      true,
      "allow-null-check"
    ],
    "typedef-whitespace": [
      true,
      
        "call-signature": "nospace",
        "index-signature": "nospace",
        "parameter": "nospace",
        "property-declaration": "nospace",
        "variable-declaration": "nospace"
      
    ],
    "typeof-compare": true,
    "unified-signatures": true,
    "variable-name": false,
    "whitespace": [
      true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-separator",
      "check-type"
    ],
    "directive-selector": [
      true,
      "attribute",
      "app",
      "camelCase"
    ],
    "component-selector": [
      true,
      "element",
      "app",
      "kebab-case"
    ],
    "use-input-property-decorator": true,
    "use-output-property-decorator": true,
    "use-host-property-decorator": true,
    "no-input-rename": true,
    "no-output-rename": true,
    "use-life-cycle-interface": true,
    "use-pipe-transform-interface": true,
    "component-class-suffix": true,
    "directive-class-suffix": true,
    "invoke-injectable": true
  

【问题讨论】:

从这里开始:update.angular.io。选择适用于您的选项后,按照列出的步骤操作。 关注了它,但是当我进行 ng update @angular/cli 我收到错误提示 你的angular.json文件呢? angular-cli.json ?也是有效的 json 格式 您提到全局安装 CLI,这很好。你也是本地安装的吗?您的帖子中没有提到该步骤。 【参考方案1】:

只是为了确保运行以下步骤:

npm uninstall -g @angular/cli
npm cache verify
npm install -g @angular/cli@latest

然后在你的本地项目包中:

rm -rf node_modules dist 
npm install --save-dev @angular/cli@latest
npm i 
ng update @angular/cli 
ng update @angular/core

如果这不起作用,我认为你应该手动更新 package.json

  "dependencies": 
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  ,
  "devDependencies": 
    "@angular/compiler-cli": "^6.0.3",
    "@angular-devkit/build-angular": "~0.6.5",
    "typescript": "~2.7.2",
    "@angular/cli": "~6.0.5",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~1.4.2",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  

删除 .angular-cli.json 并在手动更新时创建 angular.json

  ng update @angular/cli --migrate-only --from=1.7.4

【讨论】:

作为最后一步而不是手动编辑运行:ng update @angular/cli【参考方案2】:

试试这个答案:https://***.com/a/50965027/1002641

问题是在 /usr/local 下安装了 ng(以及 /usr/local/bin 中的符号链接)。删除它,帮助我将我的 @angular/cli 版本更新为 6(从 1.7.1 开始)

【讨论】:

感谢您的回答。删除符号链接 /usr/local/bin 修复了我的问题。非常感谢。

以上是关于无法将角度更新到版本 6的主要内容,如果未能解决你的问题,请参考以下文章

无法更新角度版本

无法以角度更新子组件属性

防止凉亭更新角度版本

更新到角度6后找不到app-root

从 5 到 6 的角度迁移后无法构建 - 找不到模块“打字稿”

角度更新 10.2 到 11