Angular CLI:无法读取null

时间:2017-04-27 18:07:00

标签: angular angular-cli

最近将我的应用程序从Angular 2.3.0升级到Angular 4.0.3。在此过程中还升级了angular-cli。如果项目克隆到新目录,则抛出:

  

无法读取null的属性'config'TypeError:无法读取属性   'config'为null       在Class.run(/Users/hyadav/Documents/projects/web-app/node_modules/@angular/cli/tasks/build.js:16:56)       在Class.run(/Users/hyadav/Documents/projects/web-app/node_modules/@angular/cli/commands/build.js:143:26)       在课堂上。 (/Users/hyadav/Documents/projects/web-app/node_modules/@angular/cli/ember-cli/lib/models/command.js:134:17)       at process._tickCallback(internal / process / next_tick.js:109:7)

的package.json

"scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.3",
    "@angular/cli": "^1.0.1",
    "@angular/common": "^4.0.3",
    "@angular/compiler": "^4.0.3",
    "@angular/compiler-cli": "^4.0.3",
    "@angular/core": "^4.0.3",
    "@angular/forms": "^4.0.3",
    "@angular/http": "^4.0.3",
    "@angular/material": "^2.0.0-beta.3",
    "@angular/platform-browser": "^4.0.3",
    "@angular/platform-browser-dynamic": "^4.0.3",
    "@angular/platform-server": "^4.0.3",
    "@angular/router": "^4.0.3",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "rxjs": "^5.2.0",
    "typescript": "^2.2.2",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "^1.0.1",
    "@angular/compiler-cli": "^2.4.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "~6.0.60",
    "codelyzer": "~2.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",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.4.2",
    "typescript": "~2.2.2"
 }

5 个答案:

答案 0 :(得分:16)

这是一个有效的解决方案,直到Angular v5,因为在v6中,文件的位置更改为angular.json,并且还更改了内容

创建一个名为 .angular-cli.json 的新文件并插入:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "PROJECT_NAME"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.app.json",
      "testTsconfig": "tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "styles.css"
      ],
      "scripts": [],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "lint": [
    {
      "project": "src/tsconfig.app.json"
    },
    {
      "project": "src/tsconfig.spec.json"
    },
    {
      "project": "e2e/tsconfig.e2e.json"
    }
  ],
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "component": {}
  }
}

如果您从angular-cli创建新项目,也会找到此json的副本。

答案 1 :(得分:9)

通过将.angular-cli.json文件复制到新目录来修复此问题。

每个请求,更多详情:

创建项目后,但在将其移动到其他目录之前,您会注意到已创建.angular-cli.json文件,可通过命令行ls -al显示。项目的根目录中需要此文件。它应该通过mv .angular-cli.json .移动到那里,并且应该添加到源代码控制中,以便其他克隆项目的人都有这个文件。

答案 2 :(得分:4)

我找到了解决方案! :D

  

在我的情况下,我将项目移动到另一个目录       有一个名为.angular-cli,

的隐藏文件      
    

显示旧项目中的隐藏文件并复制此文件并粘贴它         在新目的地。

  

这解决了我的问题。

答案 3 :(得分:0)

别忘了安装npm软件包,
在根目录中运行npm i

答案 4 :(得分:0)

在angular 5之后,当我们创建项目的zip或副本时,那时 angular-cli.json文件未包含在zip或副本文件夹中,我们将此zip或文件夹发送给另一个朋友或本身,但安装角度项目后。我们运行ng serve,但是很遗憾,它给出错误无法读取null的属性配置

因此要解决此问题,还发送 angular-cli.json 文件,或者您可以将其复制并粘贴到新项目(错误项目)中。

* angular-cli.json = 隐藏文件 .....必须记住。