【问题标题】:Angular 2 CLI Upgraded to latest and now ng serve gives me errorsAngular 2 CLI 升级到最新版本,现在 ng serve 给了我错误
【发布时间】:2016-09-01 18:07:49
【问题描述】:

我在当前的 Angular 2 CLI 项目中升级到 1.0.0-beta.11-webpack.8,并更新了我的 angular-cli.json 以匹配升级后 CLI 的预期属性。当我运行 ng serve 时,我得到了这个错误:

`Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at Object.getWebpackCommonConfig (/Applications/MAMP/htdocs/learnloop/node_modules/angular-cli/addon/ng2/models/webpack-build-common.ts:23:25)
    at new NgCliWebpackConfig (/Applications/MAMP/htdocs/learnloop/node_modules/angular-cli/addon/ng2/models/webpack-config.ts:13:30)
    at Class.exports.default.Task.extend.run (/Applications/MAMP/htdocs/learnloop/node_modules/angular-cli/addon/ng2/tasks/serve-webpack.ts:19:22)
    at /Applications/MAMP/htdocs/learnloop/node_modules/angular-cli/addon/ng2/commands/serve.ts:84:26
    at process._tickCallback (internal/process/next_tick.js:103:7)`

我为此苦苦挣扎了一段时间,但是在运行了所有这些命令并让 Angular CLI 不处于损坏状态之后,我遇到了这个问题。以下是我运行的命令:

`npm uninstall -g angular-cli
npm cache clean
npm install -g angular-cli@webpack
rm -rf node_modules dist tmp typings
npm install --save-dev angular-cli@webpack`

最后,这是我正在运行的 node、npm 和 Angular CLI 的版本:

Node v6.4.0 NPM 3.10.3 angular-cli: 1.0.0-beta.11-webpack.8

这是我的 angular-cli.json:

`{
  "project": {
    "version": "1.0.0-beta.11-webpack.8",
    "name": "learnloop"
  },
  "apps": [
    {
      "main": "src/main.ts",
      "tsconfig": "src/tsconfig.json",
      "mobile": false,
      "prefix": "app",
      "root": "src",
      "styles": [
        "styles.scss"
      ]
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "config/protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "config/karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "prefixInterfaces": false,
    "lazyRoutePrefix": "+"
  }
}`

对于 huuuuge 的文字墙,我深表歉意,但我想确保你们所有人都拥有可能帮助解决此问题所需的一切。我在这里做错了什么?

【问题讨论】:

  • 更新到当前的 angular-cli 后,我遇到了同样的问题。

标签: angular angular-cli


【解决方案1】:

我设法让它工作。尝试将其粘贴到您的 angular-cli.json 中(相应地对其进行编辑):

{
  "project": {
    "version": "1.0.0-beta.11-webpack.8",
    "name": "<name-of-app>"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": "assets",
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.scss"
      ],
      "scripts": [],
      "environments": {
        "source": "environments/environment.ts",
        "prod": "environments/environment.prod.ts",
        "dev": "environments/environment.dev.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "scss",
    "prefixInterfaces": false,
    "lazyRoutePrefix": "+"
  }
}

然后在根项目目录中输入ng init。它会要求您替换一堆不同的文件或保留它们。文件之间存在一些细微差别,尤其是在 app.module.ts 内部,因此请确保您先提交,因为文件内部的文件结构和代码会有一些变化。

【讨论】:

  • @Steve Star 该文件与 angular-cli 当前版本生成的文件相同,但仍然可以看到控制台输出。
【解决方案2】:

尝试将您的 nodejs 版本更新到最新版本(当前为 v.7.2)。它对我有用,但 ng serve 失败了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2021-11-26
    • 2019-02-06
    • 1970-01-01
    • 2020-04-09
    • 2016-08-26
    • 1970-01-01
    相关资源
    最近更新 更多