I’m trying to deploy a simple smart contract using Ethers.js with a local Ganache node, but I’m encountering an issue that I can’t seem to resolve.
Here is my current code:
const ethers = require("ethers");
const fs = require("fs-extra");
async function main() {
// HTTP://127.0.0.1:7545
const provider = new ethers.providers.JsonRpcProvider("http://127.0.0.1:7545");
const wallet = new ethers.Wallet(
"0x0e5c2e02091863ed2118a76342610dec3482fde540806bdec108a12d16e97860",
provider
);
const abi = fs.readFileSync(
"/Users/Work/Code/block/ethers-simple-storage/SimpleStorage_sol_SimpleStorage.abi",
"utf-8"
);
const binary = fs.readFileSync(
"/Users/Work/Code/block/ethers-simple-storage/SimpleStorage_sol_SimpleStorage.bin",
"utf-8"
);
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("deploying");
const contract = await contractFactory.deploy();
console.log(contract);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Error Encountered: When I run this script, it gives me this error:
TypeError: Cannot read properties of undefined (reading ‘JsonRpcProvider’)
at main (/Users/Work/Code/block/ethers-simple-storage/deploy.js:10:41)
at Object.<anonymous> (/Users/Work/Code/block/ethers-simple-storage/deploy.js:31:1)
at Module._compile (node:internal/modules/cjs/loader:1358:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1416:10)
at Module.load (node:internal/modules/cjs/loader:1208:32)
at Module._load (node:internal/modules/cjs/loader:1024:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49
#Environment
Node.js version: v14.17.0
Ethers.js version: ^5.4.0
Ganache CLI version: v6.12.2
Operating System: macOS Big Sur 11.4
What I’ve Tried:
Verified that Ganache is running and accessible at http://127.0.0.1:7545.
Checked the ABI and binary paths; they seem to be correct.
Confirmed the wallet address and private key are correct and have sufficient funds.
I have also tried downgrading ethers but it was not fruitful.
i also tried using let provider = new ethers.JsonRpcProvider(process.env.RPC_URL)
#Questions:
How should I run this script
How can i keep up with the new updates as i am learning from an older course this is a good one but i dont seem to understand how to bypass the update barrier
https://www.youtube.com/watch?v=gyMwXuJrbJQ&list=PPSV
Any help or pointers would be greatly appreciated!
Aryaman Gupta is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.