首页 > 解决方案 > ReferenceError:全局未使用 web3 定义

问题描述

一段时间以来,我一直在尝试将 Web3 安装到我的 Ionic v4 项目中。当我为项目服务时,它不断抛出错误。我收到一条错误消息,指出 Reference Error: global is not defined。

  1. 开始一个新的 Ionic 项目
  2. 安装 web3 : npm install --save web3
  3. 安装节点类型: npm install --save-dev @types/node
  4. 编辑一些代码以访问 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);
  }
}
  1. 运行离子服务
  2. 编译成功,但我在谷歌浏览器的控制台中遇到这样的错误
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)

包.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 项目?

标签: javascriptnode.jsangulartypescriptionic-framework

解决方案


不确定 ionic 是否有自己的 polyfill,但您可以将其添加到您的 polyfill 中

(window as any).global = window;
if (global === undefined) {
   var global = window;
}

推荐阅读