Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Web3 is not a constructor

Tags:

node.js

web3js

I'm trying to initialize web3 version 4.1.1 in my backend server, the app I'm trying to build offers the aspect of not needing the customers to have a metamask wallet, instead, any interaction with the blockchain will be using the app's wallet (company's).

I'm facing difficulties initializing a web3 instance in my backend server. Here's the relevant code and error:

const web3 = new Web3(process.env.INFURA_URL);
             ^

TypeError: Web3 is not a constructor

Any ideas?

I tried initializing web3 in my backend server so that any and all interactions with the blockchain is done using the app's wallet, eliminating the need for the customer having to have a wallet.

like image 985
Ahmad Avatar asked Jun 15 '26 10:06

Ahmad


2 Answers

For the latest version of web3, you have to set it up like:

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

NOT:

const Web3 = require('web3')

With the curly brackets around it.

like image 150
Theresa Whynot Avatar answered Jun 17 '26 23:06

Theresa Whynot


For web3(v4.x), you have to import it by:

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

for web3(v1.x) or older:

const Web3 = require('web3');

Then you can initialize it by:

const web3 = new Web3(process.env.INFURA_URL);
like image 36
Monarth Sarvaiya Avatar answered Jun 18 '26 01:06

Monarth Sarvaiya