Angular CLI 1.7.4 TypeError:无法读取null的属性“ config”

时间:2018-08-07 15:09:19

标签: angular-cli

只需将全局CLI从Angular 6降级到Angular 5,即可运行CLI 1.7.4

尝试ng为最初为Ang 5创建的项目提供服务(但降级了对Ang 6的依赖性),但出现错误:

TypeError: Cannot read property 'config' of null

at Class.run (/Users/me/git-projects/wra-starter-ng5/node_modules/@angular/cli/tasks/serve.js:51:63)
at check_port_1.checkPort.then.port (/Users/me/git-projects/wra-starter-ng5/node_modules/@angular/cli/commands/serve.js:123:26)
at process._tickCallback (internal/process/next_tick.js:68:7)

我的ng --version显示:

Angular CLI: 1.7.4
Node: 10.2.1
OS: darwin x64
Angular: 5.2.11
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cdk: 5.2.5
@angular/cli: 1.7.4
@angular/flex-layout: 2.0.0-beta.12
@angular/material: 5.2.5
@angular/upgrade: 5.2.1
@angular-devkit/architect: 0.6.8
@angular-devkit/build-angular: 0.6.8
@angular-devkit/build-optimizer: 0.6.8
@angular-devkit/core: 0.6.8
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 6.0.8
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.6.2
webpack: 4.8.3

寻找可能的原因,我猜项目的package.json与CLI 1.7.4不兼容(该项目最初是为Angular 6编写的)

这是task / serve.js中的行,它抱怨(配置为空):

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

注意:我的项目有一个angular.json但没有angular-cli.json。将angular.json重命名为angular-cli.json并从一个已知在1.7.4 CLI下工作的类似项目中借用内容会引发许多错误。

以下是我在ng上看到的错误,它们在项目中使用默认的angular-cli.json:

ERROR in ./src/main.ts
Module build failed: Error: Angular Compiler was detected but it was an instance of the wrong class.
This likely means you have several @ngtools/webpack packages installed. You can check this with `npm ls @ngtools/webpack`, and then remove the extra copies.
    at Object.ngcLoader (/Users/me/git-projects/wra-starter-ng5/node_modules/@ngtools/webpack/src/loader.js:33:15)
ERROR in ./src/polyfills.ts
Module build failed: Error: Angular Compiler was detected but it was an instance of the wrong class.
This likely means you have several @ngtools/webpack packages installed. You can check this with `npm ls @ngtools/webpack`, and then remove the extra copies.
    at Object.ngcLoader (/Users/me/git-projects/wra-starter-ng5/node_modules/@ngtools/webpack/src/loader.js:33:15)
ERROR in multi ./src/styles.css
Module not found: Error: Can't resolve '/Users/me/git-projects/wra-starter-ng5/src/styles.css' in '/Users/me/git-projects/wra-starter-ng5'
ERROR in node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.
node_modules/@ruf/shell/node_modules/@angular/core/src/render3/ng_dev_mode.d.ts(9,11): error TS2451: Cannot redeclare block-scoped variable 'ngDevMode'.
src/app/app.module.ts(7,10): error TS2305: Module '"/Users/me/git-projects/wra-starter-ng5/node_modules/@ruf/shell/ruf-shell"' has no exported member 'RufShellModule'.

1 个答案:

答案 0 :(得分:0)

我们最近在1.7.4上遇到了同样的问题。

首先尝试./node_modules/.bin/ng init(已修复几次)

您需要做的是检查您的 node_modules / angular / cli / models / config.ts 文件。

在代码上搜索此行

export const CLI_CONFIG_FILE_NAME = '.angular-cli.json';

确保有一个“。”在此文件上angular-cli.json的前面。有时cli上会出现一些未添加相对文件名的问题。

参考-(https://github.com/angular/angular-cli/issues/4736#issuecomment-280404750

确认后,

您需要从项目中删除node_modules文件夹。

然后运行npm remove webpack

删除package-lock.json文件。

npm cache clean --force

npm install @angular/cli@1.7.4

运行npm install again

最后检查是否发现任何依赖项不匹配npm ls --depth 0

运行npm start或ng服务。

如果您想这样做,可以创建新的angular cli项目,将package.json文件,src文件夹,angular-cli.json和env配置复制到新项目中。

它也应该解决该问题。