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

9

7

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?

Capitulary answered 10/7, 2022 at 10:31 Comment(0)
T
4

https://github.com/smartcontractkit/full-blockchain-solidity-course-js/blob/main/chronological-updates.md#using-the-wsl-endpoint

Please follow along one of the 4 steps that suit you on the weblink above.

Let me know if it helped.

Thanks Manu

Twowheeler answered 10/7, 2022 at 20:56 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. โ€“ Habitation
B
10

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

Bride answered 27/7, 2022 at 14:21 Comment(0)
T
4

https://github.com/smartcontractkit/full-blockchain-solidity-course-js/blob/main/chronological-updates.md#using-the-wsl-endpoint

Please follow along one of the 4 steps that suit you on the weblink above.

Let me know if it helped.

Thanks Manu

Twowheeler answered 10/7, 2022 at 20:56 Comment(1)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. โ€“ Habitation
S
1

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

Skyrocket answered 7/8, 2022 at 9:46 Comment(0)
S
1

If you are following the Freecodecamp pattric collins tutorial and you are working in the WSL ubuntu.

This is what worked for me ๐Ÿ‘ OPTION 1: If you want to install & run Ganache on your WSL itself๐Ÿ‘‡

  1. Install Ganache on WSL yarn add ganache

  2. Run Ganache yarn run ganache

  3. Copy the RPC URL (127.0.0.1:8545) from the bottom of the terminal screen that says RPC listening on 127.0.0.1:8545 & paste it in the ethers.js JsonRpcProvider( ) function parameter.

  4. Also copy any Private key on scrolling up the Terminal Screen & paste it in the ethers.js Wallet( ) function parameter.

  5. Now let this terminal run in the background, since we want our Ganache server to be running. So open a new terminal & run the deploy.js file by running the below command: node deploy.js

OPTION 2: If you want to run Ganache on your Windows ( if you have already installed Ganache on Windows )๐Ÿ‘‡

  1. Go to Settings ( Gear Icon ) on the Top Right hand Corner of Ganache Windows GUI. Move over to the Server tab and Change the HOSTNAME to WSL ( Mine worked for VirtualBox & WiFi too ) Port Number & Network ID can be LEFT UNHARMED.

  2. Click on Save & Restart at the top right hand corner of the UI.

  3. Now Copy the RPC URL from the RPC SERVER and paste it in the ethers.js JsonRpcProvider( ) function parameter.

  4. Also Copy any Private Key and paste it in the ethers.js Wallet( ) function parameter.

  5. IMPORTANT STEP ( Firewall Settings ) : Go to Windows Defender Firewall with Advanced Security on your Windows. Click on Inbound Rules & Search for Ganache Right click on Ganache & Go to Properties. Then Check the CheckBox that says Allow the connection & Click on Apply. And Close this Firewall window.

  6. Run the deploy.js file by running the command in the WSL terminal window: node deploy.js

Sidereal answered 12/1, 2023 at 17:48 Comment(0)
L
1

Bad Internet Connection

This also happens when you lose network connection. When you cannot successfully send transaction requests to the external node providers (Alchemy, Infura, etc.), then you may see this error even if all config is OK.

Lattice answered 4/9, 2023 at 12:16 Comment(0)
U
0

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.

Uvula answered 13/7, 2022 at 10:8 Comment(0)
S
0

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

Sword answered 21/7, 2022 at 6:28 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. โ€“ Palestrina
A
0

For MacOS users that encounter this error, just get Ganache up and running and try to run again with node.

Acreinch answered 4/11, 2022 at 14:55 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. โ€“ Palestrina
A
0

For me in React Native and ethers.js this solved the issue: https://github.com/ethers-io/ethers.js/issues/1995#issuecomment-2100354985

patch node_modules/@ethersproject/web/lib/browser-geturl.js

// ...
-var bytes_1 = require("@ethersproject/bytes");
+var strings_1 = require("@ethersproject/strings");
// ...
                case 1:
                    response = _a.sent();
-                    return [4 /*yield*/, response.arrayBuffer()];
+                    return [4 /*yield*/, response.text()];
                case 2:
                    body = _a.sent();
                    headers = {};
                    if (response.headers.forEach) {
                        response.headers.forEach(function (value, key) {
                            headers[key.toLowerCase()] = value;
                        });
                    }
                    else {
                        ((response.headers).keys)().forEach(function (key) {
                            headers[key.toLowerCase()] = response.headers.get(key);
                        });
                    }
                    return [2 /*return*/, {
                            headers: headers,
                            statusCode: response.status,
                            statusMessage: response.statusText,
-                            body: (0, bytes_1.arrayify)(new Uint8Array(body)),
+                            body: strings_1.toUtf8Bytes(body),
                        }];

Tried to polyfill using isomorphic-unfetch or node-fetch but with no success

Aam answered 8/5 at 11:49 Comment(0)

© 2022 - 2024 โ€” McMap. All rights reserved.