【问题标题】:Issue when running ng build运行 ng build 时出现问题
【发布时间】:2017-09-22 15:41:47
【问题描述】:

我刚刚完成了我的第一个 Angular 4 项目,只使用了用户登录。但是,当我运行ng build --prod 时,我收到以下错误:

Versions of @angular/compiler-cli and typescript could not be determined.
The most common reason for this is a broken npm install.

Please make sure your package.json contains both @angular/compiler-cli and typescript in
devDependencies, then delete node_modules and package-lock.json (if you have one) and
run npm install again.

但是,我做了上述建议的事情,我仍然得到同样的错误。我不知道从哪里开始。

Angular 在我的 MacBook Pro 上本地安装,API 是一个后端 API 调用,所有设置都已完成。 编辑 编辑 编辑 package.json的内容

    {
  "name": "angular2-registration-login-example",
  "version": "1.0.0",
  "repository": {
    "type": "git",
    "url": "https://github.com/cornflourblue/angular2-registration-login-example.git"
  },
  "scripts": {
    "start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
    "lite": "lite-server",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "core-js": "^2.4.1",
    "rxjs": "^5.2.0",
    "systemjs": "^0.19.47",
    "zone.js": "^0.8.5"
  },
  "devDependencies": {
    "@angular/cli": "^1.4.3",
    "@angular/compiler-cli": "^4.4.3",
    "@types/node": "^6.0.60",
    "concurrently": "^3.1.0",
    "lite-server": "^2.3.0",
    "typescript": "^2.2.2"
  }
}

编辑 完成建议的安装 angular/compiler-cli --save-dev 后,我得到了这个:

ng build --prod

To disable this warning run "ng set --global warnings.typescriptMismatch=false".

Cannot read property 'config' of null
TypeError: Cannot read property 'config' of null
    at Class.run (/Users/adamcampbell/Documents/Development/appDir/member/node_modules/@angular/cli/tasks/build.js:15:56)
    at Class.run (/Users/adamcampbell/Documents/Development/appDir/member/node_modules/@angular/cli/commands/build.js:204:26)
    at resolve (/Users/adamcampbell/Documents/Development/appDir/member/node_modules/@angular/cli/ember-cli/lib/models/command.js:273:20)
    at Class.validateAndRun (/Users/adamcampbell/Documents/Development/appDir/member/node_modules/@angular/cli/ember-cli/lib/models/command.js:251:12)
    at Promise.resolve.then.then (/Users/adamcampbell/Documents/Development/appDir/member/node_modules/@angular/cli/ember-cli/lib/cli/cli.js:154:24)

【问题讨论】:

  • 你能粘贴你的package.json吗?
  • @moraya.re - 完成
  • 请检查并告诉我。
  • 这是生产代码吗? git repo 可以看吗?
  • 不是生产代码。目前仅在本地。让我看看我是否可以推送到 git。等等。

标签: angular npm


【解决方案1】:

您的package.json 没有安装complier-cli。通过运行此命令 -> 安装它。

npm install @angular/compiler-cli --save-dev

【讨论】:

  • 你安装了吗?
  • 是的,对不起,请再次查看上面的编辑。
  • @webdevsoup 您是否安装了命令建议的 typescript 版本?
  • 是的,我做到了。我更新了我的编辑。这是安装Typescript后的结果
  • @webdevsoup 但我在你的package.json 中没有看到@angular/compiler-cli
【解决方案2】:

您需要将 .angular-cli.json 文件复制到应用程序的根文件夹中。

从 /node_modules/@angular/cli/tasks/build.js 的第 15 行跟踪您的错误代码

const config = config_1.CliConfig.fromProject().config;

进一步追踪,我们发现 config_1,它在第 9 行定义

const config_1 = require("../models/config");

所以我们转到 ../models/config.js ,在那里我们发现 fromProject() 如果找不到配置文件则返回 null,它指向这里:

exports.CLI_CONFIG_FILE_NAME = '.angular-cli.json';
const CLI_CONFIG_FILE_NAME_ALT = 'angular-cli.json';

因此,如果您在应用的根目录中没有看到 .angular-cli.json,则需要将其复制到那里。

【讨论】:

    【解决方案3】:

    嗯,这个错误可能会出现在很多情况下,比如。

    aot/prod 构建问题

    添加/安装reflect-metadata 将解决问题。任何在 angular 4 之后遇到问题并拥有 cli 的人都可能对此有所帮助。

    npm i --save-dev reflect-metadata
    

    你升级了系统中的节点和 npm

    在这种情况下,节点模块是旧的,它会在您的项目中显示错误。您可以删除 node_modules 文件夹和 package-lock.josn 并执行 npm install 即可。

    节点模块或同步中的一些问题

    只需删除 node_modules 文件夹和 package.json 文件并执行npm i 一段时间即可解决问题。

    他们的问题在于 cli 版本、缺少依赖项等。

    检查所有版本,如 node、npm cli 等,看看它们是否可兼容。

    这种问题主要发生在我们升级某些东西时。

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 2020-08-23
      • 1970-01-01
      • 2019-09-19
      • 2017-07-04
      • 1970-01-01
      相关资源
      最近更新 更多