运行ng build

时间:2017-09-22 03:09:27

标签: angular npm

我刚刚完成了我的第一个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"
  }
}

EDIT 在完成建议的安装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)

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

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

进一步追踪,我们找到第9行定义的config_1

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,则需要将其复制到那里。

答案 2 :(得分:0)

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

aot / prod构建问题

添加/安装reflect-metadata将解决问题。任何人在角度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等,看看它们是否可以隔离。

这种问题主要发生在升级时。