Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)

Viewed 2542

I want to connect to ganache and I get an error that I don't know how to solve it. here is my deploy.js code which is script node.js

    const ethers = require("ethers")
    // const solc = require("solc")
    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(
        //     "1ef159aa4f7ee5116a0f22652da709ccd8d35827b12a03eb24f22dd023591987",
        //     provider
        // )
        let provider = new ethers.providers.JsonRpcProvider("http://0.0.0.0:7545")
        let wallet = new ethers.Wallet("34db8b76c03356b47da8e1dcf427a35f9c013ce95a671ce23fe96d86fe2ba44b", 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(); // Stop here! wait for contract to be deploy
        console.log(contract)
    
    }
    
    main()
        .then(() => process.exit(0))
        .catch((error)=>{
            console.error(error);
            process.exit(1);
        })

and it produces the following error:

     Deploying, please wait..
     Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)
         at Logger.makeError (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/logger/lib/index.js:233:21)
         at Logger.throwError (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/logger/lib/index.js:242:20)
         at JsonRpcProvider.<anonymous> (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:561:54)
         at step (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:48:23)
         at Object.throw (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:29:53)
         at rejected (/home/stars/forder/blockchain/leson5/ethers-simple-storage/node_modules/@ethersproject/providers/lib/json-rpc-provider.js:21:65)
         at processTicksAndRejections (node:internal/process/task_queues:96:5) {
       reason: 'could not detect network',
       code: 'NETWORK_ERROR',
       event: 'noNetwork'
     }

What am I doing wrong?

5 Answers

You are following the Freecodecamp pattric collins tutorial. You are working in the WSL ubuntu terminal and installed the Ganache locally and must be located in the WSL server. So, Ganache is not connected to different environment. Go to the Ganache and click on Settings, then go the server and choose WSL as server. After this save and restart the Ganache. In your code in the connection code replace this

const provider = new ethers.providers.JsonRpcProvider(
    "HTTP://172.27.224.1:7545"
  );

or with your shown JSON-RPC. It will work for you. Thanks

For me, I installed ganache-cli in the wsl and it was able to connect to ganache-cli - running directly on WSL2.

Just in case this helps.

This happens if you run the code in WSL but Ganache is opened in Windows. Follow along the URL given in another answer here. For me changing the server on ganache UI helped

If you are on windows and using WSL-UBUNTU just go to the server setting in Ganache and reset the server to:

vETHERNET - WSL

Example

enter image description here

Related