smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.25k stars 2.94k forks source link

please someone help me with this problem: cannot read properties of undefined (reading 'JsonRpcProvide') #4736

Closed dasdeepanjan closed 1 year ago

dasdeepanjan commented 1 year ago

Error generated:

TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
    at main (/home/little_boy/hh-deep/ethers-simple-storage/deploy.js:8:41)
    at Object.<anonymous> (/home/little_boy/hh-deep/ethers-simple-storage/deploy.js:27:1)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47
{
  "dependencies": {
    "ethers": "^6.0.2",
    "fs": "^0.0.1-security",
    "fs-extra": "^11.1.0",
    "solc": "0.8.8"
  },
  "scripts": {
    "compile": "yarn solcjs --bin --abi --include-path node_modules/ --base-path . -o . SimpleStorage.sol "
  }
}

deploy.js

const ethers = require("ethers");
const fs = require("fs");

async function main() {
  //compile them in our code
  //compile them seperately
  //http://127.0.0.1:7545
  const provider = new ethers.providers.JsonRpcProvider(
    "http://127.0.0.1:7545"
  );

  const wallet = new ethers.Wallet(
    "b62a143a933502886c68359e4e1a9dfcb13d7607664158d33e2f1f129367b686",
    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, please wait...");
  const contract = await contractFactory.deploy();
  console.log(contract);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Originally posted by @dasdeepanjan in https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/4735

mikakort commented 1 year ago

I had this issue a minute ago and I just wrote: const provider = new ethers.JsonRpcProvider("your url");

BukiOffor commented 1 year ago

If your version of ethers is 6.0.2 and above then try this

const provider =  new ethers.JsonRpcProvider("http://127.0.0.1:8545");

ethers V5.0.4 uses the ethers.providers.JsonRpcProvider() but I don't think it's supported in the newer version. Verson 6 Documentation Here

BukiOffor commented 1 year ago

Also, version 6 of ethers is having a bit of an issue, so it's better to install a lower version. you can use

yarn add ethers@5.0.11

to install version 5 of ethers. if you revert to version 5, remember to change your provider back to ethers.providers.JsonRpcProvider().

AstroUserName commented 1 year ago

I guess you have this problem in test file, where you didnt even use JsonRpcProvider, so yes, just downgrade ethers, remove node_modules and install again. It helped me

https://ethereum.stackexchange.com/questions/144451/typeerror-cannot-read-properties-of-undefined-reading-jsonrpcprovider/144455