ReferenceError:全局未使用 web3 定义
Posted
技术标签:
【中文标题】ReferenceError:全局未使用 web3 定义【英文标题】:ReferenceError: global is not defined with web3 【发布时间】:2019-12-13 03:38:12 【问题描述】:一段时间以来,我一直在尝试将 Web3 安装到我的 Ionic v4 项目中。当我为项目服务时,它不断抛出错误。我收到一条错误消息,指出 Reference Error: global is not defined。
-
开始一个新的 Ionic 项目
安装 web3:npm install --save web3
安装节点类型:npm install --save-dev @types/node
编辑一些代码以访问 web3:
import Component from '@angular/core';
import Web3 from 'web3';
@Component(
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
)
export class HomePage
web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/v3/apikey...'));
constructor()
test()
console.log(this.web3);
-
运行离子服务
编译成功,但在谷歌浏览器的控制台中出现这样的错误
core.js:9110 ERROR Error: Uncaught (in promise): ReferenceError: global is not defined
ReferenceError: global is not defined
at Object../node_modules/stream-http/lib/capability.js (capability.js:1)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/stream-http/lib/request.js (request.js:1)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/stream-http/index.js (index.js:1)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/xhr2-cookies/dist/xml-http-request.js (xml-http-request.js:21)
at __webpack_require__ (bootstrap:84)
at Object../node_modules/xhr2-cookies/dist/index.js (index.js:6)
at __webpack_require__ (bootstrap:84)
at resolvePromise (zone-evergreen.js:797)
at resolvePromise (zone-evergreen.js:754)
at zone-evergreen.js:858
at ZoneDelegate.invokeTask (zone-evergreen.js:391)
at Object.onInvokeTask (core.js:34182)
at ZoneDelegate.invokeTask (zone-evergreen.js:390)
at Zone.runTask (zone-evergreen.js:168)
at drainMicroTaskQueue (zone-evergreen.js:559)
package.json
"name": "my-app",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts":
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
,
"private": true,
"dependencies":
"@angular/common": "~8.1.2",
"@angular/compiler": "~8.1.2",
"@angular/core": "~8.1.2",
"@angular/forms": "~8.1.2",
"@angular/platform-browser": "~8.1.2",
"@angular/platform-browser-dynamic": "~8.1.2",
"@angular/router": "~8.1.2",
"@ionic-native/core": "^5.0.0",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^4.7.1",
"core-js": "^2.5.4",
"rxjs": "~6.5.1",
"tslib": "^1.9.0",
"web3": "^1.2.0",
"zone.js": "~0.9.1"
,
"devDependencies":
"@angular-devkit/architect": "~0.801.2",
"@angular-devkit/build-angular": "~0.801.2",
"@angular-devkit/core": "~8.1.2",
"@angular-devkit/schematics": "~8.1.2",
"@angular/cli": "~8.1.2",
"@angular/compiler": "~8.1.2",
"@angular/compiler-cli": "~8.1.2",
"@angular/language-service": "~8.1.2",
"@ionic/angular-toolkit": "~2.0.0",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^8.9.5",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.4.3"
,
"description": "An Ionic project"
我在这里找到了一个建议https://github.com/ethereum/web3.js/issues/2260#issuecomment-458519127。 我遵循了每一步,但我也遇到了同样的错误。
如何将 web3 导入 Ionic 项目?
【问题讨论】:
【参考方案1】:不确定 ionic 是否有自己的 polyfill,但您可以将其添加到您的 polyfill 中
(window as any).global = window;
if (global === undefined)
var global = window;
【讨论】:
你能提供关于 polyfill 的上下文吗?这对于 javascript 前端的新手来说并不清楚。以上是关于ReferenceError:全局未使用 web3 定义的主要内容,如果未能解决你的问题,请参考以下文章
未捕获的 ReferenceError:在 Angular 2 webpack 全局库安装中未定义要求
未捕获的 ReferenceError:全局未在 Object../node_modules/fbjs/lib/setImmediate.js 中定义
ReferenceError:使用 performance.now() 时未定义性能