Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error connecting to localhost after npm hardhat run

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)
like image 386
sultan m_c_t Avatar asked Jul 24 '21 20:07

sultan m_c_t


People also ask

How install NPM hardhat locally?

Hardhat is used through local installation in your project. To install it, create an npm project by going to an empty folder, running npm init in the terminal. Then, run npm install — save-dev hardhat to install hardhat as a development dependency. Create your Hardhat project by running npx hardhat in project folder.

What port does hardhat run on?

This will start Hardhat Network, and expose it as a JSON-RPC and WebSocket server. Then, just connect your wallet or application to http://127.0.0.1:8545 . If you want to connect Hardhat to this node, you just need to run using --network localhost .

Does hardhat use ganache?

If you don't want to manually start and stop Ganache every time, you can use the hardhat-ganache plugin. This plugin creates a network called ganache , and automatically starts and stops Ganache before and after running your tests and scripts.

What is hardhat Blockchain?

Hardhat is a development environment for Ethereum software. It consists of different components for editing, compiling, debugging and deploying your smart contracts and dApps, all of which work together to create a complete development environment.


3 Answers

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
like image 100
Zac H Avatar answered Jan 01 '23 19:01

Zac H


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
like image 27
Oli Avatar answered Jan 01 '23 18:01

Oli


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.

like image 21
Goran Stoyanov Avatar answered Jan 01 '23 18:01

Goran Stoyanov