admin管理员组

文章数量:1305230

I have problem in Web3 provider recently. I have used HttpProvider as Web3 Provider, but now I cannot use it. Previous projects are also not executed. Here is my code and Error:

Code:

const url = `/{my_api_key}`;
const web3 = new Web3(new Web3.providers.HttpProvider(url));

Error Message:

const web3 = new Web3(new Web3.providers.HttpProvider(url));
^

TypeError: Web3.providers.HttpProvider is not a constructor.
...

How can I fix it?

So I have tried to fix this error by controlling Web3 Version, changing providers, etc, but not fixed.

I have problem in Web3 provider recently. I have used HttpProvider as Web3 Provider, but now I cannot use it. Previous projects are also not executed. Here is my code and Error:

Code:

const url = `https://sepolia.infura.io/v3/{my_api_key}`;
const web3 = new Web3(new Web3.providers.HttpProvider(url));

Error Message:

const web3 = new Web3(new Web3.providers.HttpProvider(url));
^

TypeError: Web3.providers.HttpProvider is not a constructor.
...

How can I fix it?

So I have tried to fix this error by controlling Web3 Version, changing providers, etc, but not fixed.

Share Improve this question edited Jul 18, 2023 at 15:48 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Jul 18, 2023 at 15:35 AresColinAresColin 191 silver badge3 bronze badges 1
  • 1 If the url you have provided is the url you are using, you are missing a $ for the interpolation to work. It should be https://sepolia.infura.io/v3/${my_api_key}; – Phobos Commented Jul 18, 2023 at 15:56
Add a ment  | 

3 Answers 3

Reset to default 8

For me, wrapping works

replace const Web3 = require('web3)

with const {Web3} = require('web3')

Try to install web3 version 1.10.0

npm uninstall web3

then

npm i [email protected]

Install the latest web3 library version by :

npm i web3@latest

then add in your code :

const express = require('express');
const {Web3} = require('web3');

本文标签: javascriptHow to fix error Web3providersHttpProvider is not a constructorStack Overflow