Javascript 升级到 angular-6.x 会出现“Uncaught ReferenceError: global is not defined”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50356408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Upgrading to angular-6.x gives "Uncaught ReferenceError: global is not defined"
提问by Atikur Rahman
I upgraded my project from angular-5.x to angular-6.x and it started giving the following error and even creation of dummy global variable does not work as given here Angular 6 Auth0 - global not defined
我将我的项目从 angular-5.x 升级到 angular-6.x,它开始出现以下错误,甚至创建虚拟全局变量也无法像这里给出的那样工作Angular 6 Auth0 - global not defined
The error is as follows:
错误如下:
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)
after resolving this I get following error:
解决此问题后,我收到以下错误:
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)
And continues on and on.
并继续下去。
回答by Vojtech
Adding this line to polyfills.tsshould resolve node global error
将此行添加到polyfills.ts应解决节点全局错误
(window as any).global = window;
(window as any).global = window;
The solution was mentioned in this angular-cli issue thred
在这个angular-cli issue thred中提到了解决方案
回答by Akhilesh Kumar
Add following code in your starting page e.g. index.html
在起始页中添加以下代码,例如 index.html
var global = global || window;
var Buffer = Buffer || [];
var process = process || {
env: { DEBUG: undefined },
version: []
};
Example:
例子:
<!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>
Above will work on hybrid app (in Node environment) as well as in browser
以上将适用于混合应用程序(在 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: [] };
对于“未捕获的 ReferenceError:全局未定义”:
var global = global || window;对于“未捕获的 ReferenceError:缓冲区未定义”:
var Buffer = Buffer || [];对于“未捕获的引用错误:未定义进程”:
var process = process || { env: { DEBUG: undefined } }对于“未捕获的类型错误:无法读取未定义的属性‘切片’”:
var process = process || { env: { DEBUG: undefined }, version: [] };
回答by Todd Skelton
I use HttpClientand accidentally selected the default import which was 'selenium-webdriver/http'
我使用HttpClient并意外选择了默认导入'selenium-webdriver/http'
If your app.module.ts has import { HttpClient } from 'selenium-webdriver/http';
如果你的 app.module.ts 有 import { HttpClient } from 'selenium-webdriver/http';
Update it to import { HttpClient } from '@angular/common/http';
将其更新为 import { HttpClient } from '@angular/common/http';
That fixed my issue.
那解决了我的问题。
回答by Daniel Danielecki
In case, if your targetis nodein webpack (target: 'node'), because you want to fix "Can't resolve 'fs'. Then you're getting the following error Fix: "Uncaught ReferenceError: global is not defined"do it as follows node: { global: true, fs: 'empty' }. Bonus: if you got error "Uncaught ReferenceError: exports is not defined".simply add libraryTarget: 'umd'. The complete webpack config code is below.
万一,如果你的目标是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.
Many solutions have been proposed here: https://github.com/angular/angular-cli/issues/8160
这里已经提出了很多解决方案:https: //github.com/angular/angular-cli/issues/8160

