升级到 angular-6.x 可带来"Uncaught ReferenceError: global is not defined"



我将我的项目从 angular-5.x 升级到 angular-6.x,它开始出现以下错误,甚至创建虚拟全局变量也无法像这里给出的那样工作 角度 6 Auth0 - 全局未定义

错误如下:

Uncaught ReferenceError: global is not defined
at Object../node_modules/has-binary2/index.js (index.js:10)
at __webpack_require__ (bootstrap:81)
at Object../node_modules/socket.io-parser/index.js (index.js:8)
at __webpack_require__ (bootstrap:81)
at Object../node_modules/socket.io-client/lib/index.js (index.js:7)
at __webpack_require__ (bootstrap:81)
at Object../src/app/app4pc/apiConnection/services/ApiConnectionServer.ts (auth.interceptor.ts:8)
at __webpack_require__ (bootstrap:81)
at Object../src/app/app4pc/apiConnection/toServer.module.ts (ApiConnectionServer.ts:11)
at __webpack_require__ (bootstrap:81)

解决此问题后,我收到以下错误:

Uncaught ReferenceError: process is not defined
at Object../node_modules/process-nextick-args/index.js (index.js:3)
at __webpack_require__ (bootstrap:81)
at Object../node_modules/readable-stream/lib/_stream_readable.js (_stream_readable.js:26)
at __webpack_require__ (bootstrap:81)
at Object../node_modules/readable-stream/readable-browser.js (readable-browser.js:1)
at __webpack_require__ (bootstrap:81)
at Object../node_modules/simple-peer/index.js (index.js:7)
at __webpack_require__ (bootstrap:81)
at Object../src/app/util/services/call.services.ts (notification.service.ts:12)
at __webpack_require__ (bootstrap:81)    

并继续下去。

将此行添加到polyfills.ts应该可以解决节点全局错误

(window as any).global = window;

在这个角度 cli 问题中提到了解决方案。

在起始页中添加以下代码,例如索引.html

var global = global || window;
var Buffer = Buffer || [];
var process = process || {
env: { DEBUG: undefined },
version: []
};

例:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Client</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script>
var global = global || window;
var Buffer = Buffer || [];
var process = process || {
env: { DEBUG: undefined },
version: []
};
</script>
</head>
<body>
<app-root>
<div class="loader"></div>
</app-root>
</body>
</html>

以上将适用于混合应用程序(在节点环境中)以及浏览器

  • 对于"未捕获的引用错误:未定义全局":

    var global = global || window;
    
  • 对于"未捕获的引用错误:未定义缓冲区":

    var Buffer = Buffer || [];
    
  • 对于"未捕获的引用错误:未定义进程":

    var process = process || {
    env: { DEBUG: undefined }
    }
    
  • 对于"未捕获的类型错误:无法读取未定义的属性'切片'":

    var process = process || {
    env: { DEBUG: undefined },
    version: []
    };
    

我使用HttpClient,不小心选择了默认导入,该导入'selenium-webdriver/http'

如果你的应用模块有import { HttpClient } from 'selenium-webdriver/http';

将其更新为import { HttpClient } from '@angular/common/http';

这解决了我的问题。

将此行添加到pollyfills.ts中解决了我的问题(只是@kemalbirinci在这里所说的解决方法)

(window as any).global = window;

如果你的目标是webpack 中的节点(target: 'node'),因为你想修复"Can't resolve 'fs'.然后,您会收到以下错误Fix: "Uncaught ReferenceError: global is not defined"请执行以下操作node: { global: true, fs: 'empty' }奖励:如果您遇到错误"Uncaught ReferenceError: exports is not defined".只需添加libraryTarget: 'umd'。完整的 webpack 配置代码如下。

const webpackConfig = {
node: { global: true, fs: 'empty' }, // Fix: "Uncaught ReferenceError: global is not defined", and "Can't resolve 'fs'".
output: {
libraryTarget: 'umd' // Fix: "Uncaught ReferenceError: exports is not defined".
}
};
module.exports = webpackConfig; // Export all custom Webpack configs.

这里提出了许多解决方案:https://github.com/angular/angular-cli/issues/8160

请小心使用将内联脚本添加到索引.html的最高答案。 它将解决眼前的问题。 但是,如果您使用的是 SignalR,这将生成此错误:

错误:解析握手响应时出错:类型错误:"实例"的右侧不可调用 at HubConnection.push../node_modules/@microsoft/signalr/dist/esm/HubConnection.js.HubConnection.processHandshakeResponse

但是,仅自行定义全局即可,而不会破坏信号 R。

就我而言,Package.JSON文件中缺少 socket.io 条目。请检查它并将其安装到软件包中。

迁移到 Angular v6 后,在 polyfills.ts 中添加以下行对我来说效果很好。

(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');

答案从这里取

我不知道这是否会帮助任何人了解他们的问题,我只是想我会通知任何在用户端收到全局未定义错误的阅读者检查您的浏览器和/或设备设置中的页面询问您的位置。我刚刚进入了chrome浏览器的网站设置,并将位置选项从询问首先转到阻止。我访问的下一页,数据表的主要内容显示此错误消息,当我在chrome网站设置中将位置切换回第一个询问并刷新它加载的页面而没有错误时。

最新更新