更新到 Angular 8 CLI 后抛出“.getColorDepth 不是函数”

After update to Angular 8 CLI throws ".getColorDepth is not a function"

我的一位同事将我们的项目升级到angular 8。我拉了他的分支并运行 npm install。在他的分支上一切正常。我现在每次 运行 任何 "ng ..." 命令时都会得到同样的错误:

C:\xxx\party-ui\node_modules\@angular\cli\utilities\color.js:15
    process.stdout.getColorDepth() > 1;
                   ^

    TypeError: process.stdout.getColorDepth is not a function
        at Object.<anonymous> (C:\xxx\party-ui\node_modules\@angular\cli\utilities\color.js:15:20)
        at Module._compile (module.js:652:30)
        at Object.Module._extensions..js (module.js:663:10)
        at Module.load (module.js:565:32)
        at tryModuleLoad (module.js:505:12)
        at Function.Module._load (module.js:497:3)
        at Module.require (module.js:596:17)
        at require (internal/module.js:11:18)
        at Object.<anonymous> (C:\xxx\party-ui\node_modules\@angular\cli\models\analytics.js:18:17)
        at Module._compile (module.js:652:30)

我还尝试更新 npm 和节点(现在有版本 10.16.0 和 6.9.0)。 之后还删除了 node_modules 文件夹和 运行 npm install。 我能做什么?

有同样的错误,
我的版本:

Angular CLI: 8.1.1
Node: 11.14.0
OS: linux x64
Angular: 8.1.1

运行:

rm -rf node_modules
rm -rf package-lock.json
sudo npm cache clean --force
npm install

查看有问题的文件./node_modules/@angular/cli/utilities/color.js。请注意有问题的行 (15) 上方的注释:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
 const colors = require("ansi-colors");
 exports.colors = colors;
 const tty_1 = require("tty");
 // Typings do not contain the function call (added in Node.js v9.9.0)
 exports.supportsColor = process.stdout instanceof tty_1.WriteStream &&
    process.stdout.getColorDepth() > 1;
 colors.enabled = exports.supportsColor;

具体来说:

// Typings do not contain the function call (added in Node.js v9.9.0)

将 Node 升级到不低于 v9.9.0 的版本可以解决问题。

但是...

...选择升级方式 NodeJS is not necessairly straightforward. See How do I update Node.js?。一些受欢迎的答案位于页面下方很远的位置,因此值得滚动浏览。

"@angular/core": "~8.2.3" 发生错误。

我使用这些命令将节点版本更新到最新的稳定版本,它在 linux

中为我解决了这个问题
sudo npm cache clean -f
sudo npm install -g n
sudo n stable

我在我的开发机器上使用的是较旧的 node.js 版本。将我的节点版本从 8 切换到 12 为我消除了这个错误,我的项目再次开始成功构建:

“nvm 使用 12.16.3”

NVM 是一个节点版本管理器,可以让您在节点版本之间快速切换,我建议在处理多个不同节点版本的项目时使用它。

文档: https://github.com/nvm-sh/nvm