Upgrading to angular-6.x gives "Uncaught ReferenceError: global is not defined"
Upgrading to angular-6.x gives "Uncaught ReferenceError: global is not defined"
我将我的项目从 angular-5.x 升级到 angular-6.x,它开始出现以下错误,甚至创建虚拟全局变量也不起作用如此处所示
错误如下:
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)
并继续下去。
在您的起始页中添加以下代码,例如index.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>
以上内容适用于混合应用程序(在 Node 环境中)以及浏览器
for "Uncaught ReferenceError: global is not defined":
var global = global || window;
for "Uncaught ReferenceError: Buffer is not defined":
var Buffer = Buffer || [];
for "Uncaught ReferenceError: process is not defined":
var process = process || {
env: { DEBUG: undefined }
}
for "Uncaught TypeError: Cannot read property 'slice' of undefined":
var process = process || {
env: { DEBUG: undefined },
version: []
};
将此行添加到 polyfills.ts
应该可以解决节点全局错误
(window as any).global = window;
中提到了解决方案
以防万一,如果你的 target 在 webpack (target: 'node'
) 中是 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
我使用 HttpClient
并且不小心选择了默认导入 'selenium-webdriver/http'
如果您的 app.module.ts 有 import { HttpClient } from 'selenium-webdriver/http';
更新为import { HttpClient } from '@angular/common/http';
这解决了我的问题。
将此行添加到 pollyfills.ts
中解决了我的问题(只是@kemalbirinci 所说的解决方法 here)
(window as any).global = window;
就我而言,Package.JSON
文件中缺少 socket.io 条目。请检查并安装到包中。
请小心使用将内联脚本添加到 index.html 的最佳答案。它将解决眼前的问题。但是,如果您使用的是 SignalR,则会生成此错误:
错误:解析握手响应时出错:类型错误:'instanceof' 的 Right-hand 端不可调用
在 HubConnection.push../node_modules/@microsoft/signalr/dist/esm/HubConnection.js.HubConnection.processHandshakeResponse
但是,仅自行定义 global 即可,不会破坏 Signal R。
我不知道这是否会帮助任何人理解他们的问题,我只是想我会通知任何阅读这篇文章的人在用户端出现全局未定义错误来检查您的浏览器 and/or 设备设置对于询问您的位置的页面。我刚刚进入 chrome 浏览器站点设置,并将位置选项从先询问改为阻止。我访问的下一页,数据画面的主要内容显示了此错误消息,当我在 chrome 站点设置中切换回先询问位置并刷新它加载的页面时没有错误。
在迁移到 Angular v6.
后,在 polyfills.ts 中添加以下行对我来说效果很好
(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');
答案取自here
我将我的项目从 angular-5.x 升级到 angular-6.x,它开始出现以下错误,甚至创建虚拟全局变量也不起作用如此处所示
错误如下:
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)
并继续下去。
在您的起始页中添加以下代码,例如index.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>
以上内容适用于混合应用程序(在 Node 环境中)以及浏览器
for "Uncaught ReferenceError: global is not defined":
var global = global || window;
for "Uncaught ReferenceError: Buffer is not defined":
var Buffer = Buffer || [];
for "Uncaught ReferenceError: process is not defined":
var process = process || { env: { DEBUG: undefined } }
for "Uncaught TypeError: Cannot read property 'slice' of undefined":
var process = process || { env: { DEBUG: undefined }, version: [] };
将此行添加到 polyfills.ts
应该可以解决节点全局错误
(window as any).global = window;
以防万一,如果你的 target 在 webpack (target: 'node'
) 中是 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
我使用 HttpClient
并且不小心选择了默认导入 'selenium-webdriver/http'
如果您的 app.module.ts 有 import { HttpClient } from 'selenium-webdriver/http';
更新为import { HttpClient } from '@angular/common/http';
这解决了我的问题。
将此行添加到 pollyfills.ts
中解决了我的问题(只是@kemalbirinci 所说的解决方法 here)
(window as any).global = window;
就我而言,Package.JSON
文件中缺少 socket.io 条目。请检查并安装到包中。
请小心使用将内联脚本添加到 index.html 的最佳答案。它将解决眼前的问题。但是,如果您使用的是 SignalR,则会生成此错误:
错误:解析握手响应时出错:类型错误:'instanceof' 的 Right-hand 端不可调用 在 HubConnection.push../node_modules/@microsoft/signalr/dist/esm/HubConnection.js.HubConnection.processHandshakeResponse
但是,仅自行定义 global 即可,不会破坏 Signal R。
我不知道这是否会帮助任何人理解他们的问题,我只是想我会通知任何阅读这篇文章的人在用户端出现全局未定义错误来检查您的浏览器 and/or 设备设置对于询问您的位置的页面。我刚刚进入 chrome 浏览器站点设置,并将位置选项从先询问改为阻止。我访问的下一页,数据画面的主要内容显示了此错误消息,当我在 chrome 站点设置中切换回先询问位置并刷新它加载的页面时没有错误。
在迁移到 Angular v6.
后,在 polyfills.ts 中添加以下行对我来说效果很好(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');
答案取自here