Error connecting to localhost after npm hardhat run
Asked Answered
N

9

9

I am new to deploying smart contracts with hardhat and am following a tutorial at https://dev.to/dabit3/the-complete-guide-to-full-stack-ethereum-development-3j13. However, after running npx hardhat run scripts/deploy.js --network localhost, I get the following error. Any ideas on how to fix connection issues?

HardhatError: HH108: Cannot connect to the network localhost.
    Please make sure your node is running, and check your internet connection and networks config
        at HttpProvider._fetchJsonRpcResponse (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/http.ts:176:15)
        at processTicksAndRejections (node:internal/process/task_queues:93:5)
        at HttpProvider.request (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/http.ts:55:29)
        at GanacheGasMultiplierProvider._isGanache (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/gas-providers.ts:302:30)
        at GanacheGasMultiplierProvider.request (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/hardhat/src/internal/core/providers/gas-providers.ts:291:23)
        at EthersProviderWrapper.send (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/@nomiclabs/hardhat-ethers/src/internal/ethers-provider-wrapper.ts:13:20)
        at Object.getSigners (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:23:20)
        at getContractFactoryByAbiAndBytecode (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/@nomiclabs/hardhat-ethers/src/internal/helpers.ts:250:21)
        at main (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/scripts/deploy.js:17:19)
    
        Caused by: FetchError: request to http://127.0.0.1:8545/ failed, reason: connect ECONNREFUSED 127.0.0.1:8545
            at ClientRequest.<anonymous> (/Users/cuneydtasoglu/Desktop/hardhat_list/blockchain/node_modules/node-fetch/lib/index.js:1461:11)
            at ClientRequest.emit (node:events:376:20)
            at Socket.socketErrorListener (node:_http_client:490:9)
            at Socket.emit (node:events:376:20)
            at emitErrorNT (node:internal/streams/destroy:188:8)
            at emitErrorCloseNT (node:internal/streams/destroy:153:3)
            at processTicksAndRejections (node:internal/process/task_queues:80:21)
Norma answered 24/7, 2021 at 20:37 Comment(2)
Can you confirm if the npx hardhat node is still running without errors, when you're trying to run the deploy script?Aliquant
You guys ever figure this out? I'm having same issue running on mac. I have the hardhad node running w/o error. I have a feeling it's a networking config thing.Affirm
P
19

Petr has the right solution - your local test node (which you started with npx hardhat node) needs to continue running when you deploy your smart contract.

In other words, you should:

  1. Run npx hardhat node in your terminal. Leave the process running.
  2. Open a new terminal window.
  3. Run npx hardhat run [script-name] --network localhost
Pangolin answered 12/8, 2021 at 12:30 Comment(2)
My node was running and I still got the same error message. But the error message was gone when I ran 'npx hardhat run [script-name] --network hardhat'. I am wondering what is the difference between deploying to localhost and deploying to hardhat.Sinus
I'm experiencing the same issue-also using a Mac w/hardhat 2.6.7. I haven't experimented with changing the network to 'hardhat' vs localhost but in working on my development all was well last night and today I'm experiencing this issue!!? so something happened from then to now.Jeramey
Y
12

Similar issue when running the deploy script with the following:

npx hardhat run scripts/deploy.js --network localhost

I could fix this by using hardhat instead of localhost:

npx hardhat run scripts/deploy.js --network hardhat
Yila answered 10/11, 2021 at 18:33 Comment(1)
This would only work if it is sending to hardhat network, but not the localhost server that is listening created via npx hardhat node command which was originally stated in the questionTasset
B
3

I had the same issue with running:

npx hardhat run scripts/deploy.js --network localhost

and tried to change the solidity version, clean up the project, removed and installed the npm_modules from scratch but the only solution that I found is reviewing my /etc/hosts file. There was this record:

::1             localhost

which obviously was creating troubles for the hardhat server.

As a tip please note the WebSocket JSON-RPC server address when you run npx hardhat node to be sure at which url it's running.

Bootleg answered 2/11, 2021 at 10:28 Comment(3)
Hey Goran - I had the same issue and removed the ::1 localhost entry - which worked yesterday. However today, when starting hardhat npx hardhat node, I'm getting the same issue... but /etc/hosts has no entry for ::1 localhost - would you have any ideas as to what could be the issue? Thank you!Former
Hey! I think removing the record I pointed out above partially solves the problem but it's not a good practice. In my case, I recovered the ::1 localhost and still works. While debugging my problem I noticed that when running npx hardhat node I was getting this Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/ as a response and that lead me to removing the line in the hosts file. Maybe yours is different. Take a look at the output of npx hardhat node, the problem should come up there.Bootleg
Thanks for the reply Goran - funny enough I didn't realise until later that you can supply the hostname when booting up hardhat like so: npx hardhat node --hostname 127.0.0.1 - this seems to resolve the issue 🚀Former
E
3

I have had this problem. After some research, I noticed that the node version is the key. If you are using node version 17, you can downgrade it to version 16. It applies to my project.

Extinguisher answered 29/11, 2021 at 3:59 Comment(1)
Thank you ser, worked for me!Prurigo
C
2

I had this issue too, both in the context of connecting Hardhat to Ethereum and to Avalanche.

The following worked for me:

  1. Modify /etc/hosts and delete the line ::1 localhost as mentioned by others (https://mcmap.net/q/1116750/-error-connecting-to-localhost-after-npm-hardhat-run)
  2. Run npx hardhat node. This should output a line like:
Started HTTP and WebSocket JSON-RPC server at http://127.0.0.1:8545/
  1. Modify hardhat.config.ts so that the url entry under local has the same port number as the URL printed in step 2. (8545, in this case)

Your npx commands will probably work after this.

Chrissie answered 21/11, 2021 at 18:12 Comment(0)
P
2

This problem is mainly faced only by the newbies.

  1. Don't directly deploy your contract. First, you have to start your local node to deploy your contract in localhost.

  2. You can start your local eth node by typing the command :: npx hardhat node

  3. Now deploy the contract.

Precipitous answered 15/4, 2022 at 6:53 Comment(0)
A
1

You can go to the hardhat.config.js, go to the networks, localhost and change the url from the "http://localhost:8545" to "http://127.0.0.1:8545"

In my local machine, /etc/hosts ::1 localhost entry was already commented and uncommenting it did not help, instead Replacing the above url entry in the hardhar config js did.

Astute answered 18/12, 2022 at 8:12 Comment(2)
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.Comeuppance
this worked for me in Windows 10, VS Code, bash.Homologous
S
1

Enter npx hardhat node or yarn hardhat node. And then open another terminal and run npx hardhat run scripts/deploy.js --network localhost This should work

Sparkie answered 4/4, 2023 at 7:36 Comment(0)
A
0

try npx hardhat run --network hardhat [script-name] instead

Agler answered 30/7, 2022 at 7:1 Comment(1)
you may want to add reason(s) of why you think this command will work.Aziza

© 2022 - 2024 — McMap. All rights reserved.