Cannot read properties of undefined (reading 'JsonRpcProvider') #2706
-
I'm on ether.js s SimlpeStorage. I'm try to deploy my js file but getting below error \hh-fcc\ether-simple-storage> node deploy.js
TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
at main (C:\Users\Neha Kohli\Documents\blockchain\blockchain\hh-fcc\ether-simple-storage\deploy.js:6:52)
at Object.<anonymous> (C:\Users\Neha Kohli\Documents\blockchain\blockchain\hh-fcc\ether-simple-storage\deploy.js:18:1)
at Module._compile (node:internal/modules/cjs/loader:1119:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
at Module.load (node:internal/modules/cjs/loader:997:32)
at Module._load (node:internal/modules/cjs/loader:838:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:18:47 Deploy.js const ethers = require("ether");
const fs = require("fs-extra");
async function main() {
const provider = new ethers.providers.JsonRpcProvider("HTTP://127.0.0.1:7545");
const wallet = new ethers.Wallet(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ,
provider );
const abi = fs.readFileSync("./_SimpleStorage_sol_SimpleStorage.abi", "utf8");
const binary = fs.readFileSync("./_SimpleStorage_sol_SimpleStorage.bin", "utf8");
const contractFactory = new ethers.ContractFactory(ab,binary,wallet);
console.log("deploying the contract")
const contract = await contractFactory.deploy();
console.log(contract);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
}) package.json {
"dependencies": {
"ether": "^0.0.9",
"fs-extra": "^10.1.0",
"solc": "0.8.12"
},
"scripts": {
"compile": "yarn solcjs --bin --abi --include-path .node_modules --base-path . -o . SimpleStorage.sol"
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 33 replies
-
@shrutikanwal Make sure to correct these -const ethers = require("ether");
+const ethers = require("ethers"); |
Beta Was this translation helpful? Give feedback.
-
Hey @shrutikanwal for starters, change |
Beta Was this translation helpful? Give feedback.
-
I have the same error but all the things which are incorrect here are correct in my code |
Beta Was this translation helpful? Give feedback.
-
Seems like there are some issue at ethers ver.6. |
Beta Was this translation helpful? Give feedback.
-
If anyone is still having issues. After hours of trying different things, I tried this and it worked. Link below. |
Beta Was this translation helpful? Give feedback.
-
Following steps helped me:
|
Beta Was this translation helpful? Give feedback.
-
For those that still want to use version ^6.0.8 that is version 6.0 and above ethers has a documentation for it
https://docs.ethers.org/v6-beta/getting-started/
instead of downgrading to a lower version, in the version 6 and above all you have to do is this
const provider = new ethers.JsonRpcProvider("in here will be your ganache RPC server");
instead of
const provider = new ethers.provider.JsonRpcProvider("in here will be your ganache RPC server");
|
Beta Was this translation helpful? Give feedback.
-
const ethers = require("ethers");
const fs = require("fs-extra");
async function main() {
const provider = new ethers.provider.JsonRpcProvider("http://127.0.0.1:7545");
const wallet = new ethers.Wallet(
"0xb2e7a69dd3b931ca28e3b85213593c220ee389c4290a46e4cdb052c445cfab98",
provider
);
const abi = fs.readFileSync("./_SimpleStorage_sol_SimpleStorage.abi", "utf8");
const binary = fs.readFileSync(
"./_SimpleStorage_sol_SimpleStorage.bin",
"utf8"
);
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("deploying the contract...");
const contract = await contractFactory.deploy();
console.log(contract);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
}); {
"dependencies": {
"ethers": "5.6.2",
"fs-extra": "^10.1.0",
"solc": "0.8.12"
},
"scripts": {
"compile": "yarn solcjs --bin --abi --include-path .node_modules --base-path . -o . SimpleStorage.sol"
}
} Hey i am having trouble with Vscode and an error, can someone help me? |
Beta Was this translation helpful? Give feedback.
-
The fix for me was to downgrade "ethers" to version 5.4.. you can remove previous version by running
|
Beta Was this translation helpful? Give feedback.
-
There is no need to to downgrade ethers. Simply replace - const provider = new ethers.providers.JsonRpcProvider("HTTP://127.0.0.1:7545");
+ const provider = new ethers.JsonRpcProvider("HTTP://127.0.0.1:7545"); |
Beta Was this translation helpful? Give feedback.
Hey @shrutikanwal for starters, change
ether
toethers
in the lineconst ethers = require("ether");
It should be
const ethers = require("ethers");