关键依赖:require 函数的使用方式无法静态提取依赖

Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

有人在 "ng serve" 遇到以下警告吗?

WARNING in ./node_modules/@angular/compiler/src/util.js 10:24-31 Critical dependency: require function is used in a way in which dependencies cannot be statically extracted ℹ 「wdm」: Compiled with warnings.

Angular 版本控制:

Angular CLI: 6.0.8 Node: 8.11.3 OS: darwin x64 Angular: 6.0.9 ... animations, common, compiler, compiler-cli, core, forms ... http, language-service, platform-browser ... platform-browser-dynamic, router, upgrade

我尝试更新 CLI 和 Angular 但没有成功。 util.js 中的代码如下所示:

function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define("@angular/compiler/src/util", ["require", "exports"], factory);
    }
}

我收到这个错误并发现了这个: https://fluin.io/blog/critical-dependency-cannot-be-statically-extracted,作者在其中显示他收到了相同的警告。但是,我没有使用 Angular 元素,但我认为它可能与同一问题有关,所以我继续检查我是否在我的任何导入中使用了 @angular/compiler/src/core

而且我确实在这样做。 修复就像删除导入行一样简单,在我的例子中是:

import { ViewEncapsulation } from '@angular/compiler/src/core';

然后编辑器自动导入如下:

import { Component, OnInit, ViewEncapsulation } from '@angular/core';

希望对您有所帮助。

我遇到了这个错误(如标题所示)和其他几个错误,因为我试图实现第 3 方库。

简而言之,如果您遇到这些错误,请尝试查看您的第 3 方库。在我的例子中,它是来自 Scanbot.io.

的条形码扫描库

在您的应用中搜索导入内容。

您很可能错误地导入了类似于 from '@angular/compiler/foo' 的内容。

如果您从 src 路径导入,则会抛出此警告

更改组件导入语句
import { ChangeDetectionStrategy, ViewEncapsulation } from '@angular/compiler/src/core';

import {  ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';


这次导入发生在我身上:

import { Message } from '@angular/compiler/src/i18n/i18n_ast';

我有一个消息界面,但自动填充导入功能默认为上面的那个。

我遇到了同样的错误,当我错误地从 protractor 而不是 @angular/core 导入 EventEmitter 时。

import { EventEmitter } from 'protractor'; 更改为 import { EventEmitter } from '@angular/core'; 修复了它。

我发现了一个类似的问题:

./node_modules/@angular/compiler/src/util.js:10:24-31 - Warning: Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

我记得使用 stringify 而不是 JSON.stringify 并且这个自动导入(使用 vs 代码)为:

import { stringify } from '@angular/compiler/src/util';

我删除了这个自动导入并将代码更正为 JSON.stringify。

补充一下,非常相似,但似乎从 Angular 导入了一些东西。我的是这样的:

import {error} from '@angular/compiler/src/util';

我无意中输入了 throw error() 而不是 throw Error(),它导入了它并触发了警告。