【问题标题】:Is here a way to solve this error message; Web3 is not defined?这里有解决此错误消息的方法吗? web3没有定义?
【发布时间】:2021-09-06 14:51:57
【问题描述】:

js代码: ''' 需要('dotenv').config() 常量表达 = 要求('表达') const bodyParser = require('body-parser') 常量 http = 要求('http') 常量 web3_utils = 要求('web3') const HDWalletProvider = require('@truffle/hdwallet-provider') const moment = require('moment-timezone') 常量数字 = 要求('数字') const _ = 要求('lodash') const axios = require('axios')

// SERVER CONFIG
const PORT = process.env.PORT || 5500
const app = express();
const server = http.createServer(app).listen(PORT, () => console.log(`Listening on ${ 
PORT }`))

// WEB3 CONFIG
const web3 = new Web3(process.env.RPC_URL) '''

在 npm run start 后的终端中,输出为: 节点索引.js

/home/trosilez/price-bot/index.js:18
const web3 = new Web3(process.env.RPC_URL)
         ^

ReferenceError: Web3 is not defined
at Object.<anonymous> (/home/trosilez/price-bot/index.js:18:14)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:279:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:696:3)

请帮助我,我是编码新手

【问题讨论】:

    标签: javascript visual-studio visual-studio-code web3 web3js


    【解决方案1】:

    Web3 未定义,因为您在导入依赖项时没有包含变量“Web3”。

    将 web3_utils 重命名为 Web3。它应该看起来像这样。

    const Web3 = require('web3');
    
    const web3 = new Web3(process.env.RPC_URL);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2018-05-11
      • 2019-11-16
      相关资源
      最近更新 更多