I can "spin up" a local fake blockchain in Terminal using

$ hardhat node

I can also make it fork the real mainnet using Alchemy, even pin it to a specific block N. Other local applications can then connect at http://localhost:8545 and interact with the forked local chain until I press Ctrl+C in that Terminal window and my local JSON-RPC node disappears. So far so good.

My question is: How do I combine this feature with deploying a contract?

How do I do BOTH:

  1. Spin up the forked chain at block N, so it is usable via port 8545 (launch an JSON-RPC server that runs until I stop it)
  2. And while my forked chain spins up, how do I deploy a test contract "into it" (probably will increase my block number from N to N + 1)?

What I have tried:

I have tried a deploy script. It seems that I can "spin up" the desired forked chain at block N momentarily, but at no time is it accessible via port 8545 to other applications.

Inside the script, I can deploy a contract with the usual deploy commands, and even interact with my contract.

What I really need though, is a "live" (accessible via port 8545 or 8546) forked chain with my brand new contract "in it", so another app (that is not launched via hardhat or hh commands) can interact with it.

Is this possible?


Just like it was said by Franco in the previous answer you can use the --network tag to deploy to the node you're running. For easy deployment, you can use the hardhat deploy plugin.

I would like to add that you make the app you're running locally accessible to other people by running ngrok for example. Since the node you're running is on localhost:8545, you could run ngrok http 8545 which will give you a url like https://XXXX-XXXX.ngrok.io
You can then share that link with other people (they can input it into their Metamask or you could use it in your web application. It's a great way of testing your app quickly from your localhost


When you run a Hardhat task without specifying a network with the --network param, a temporary, in-process Hardhat network is created for that task. That network doesn't open any port, it's just available for that task.

The node task is a built-in task which starts an HTTP server, making the Hardhat network available (in localhost:8545 by default).

So what you can do is start a node in one terminal, and then run any other task (for example, your deploy) in another one using --network localhost. This is just a default network that connects to localhost:8545, there's nothing special about it.

I wrote a more in-depth explainer here if you want to learn more.


Exposing an RPC from an existing script can be done by calling the node task.

  1. Make sure you've setup forking for hardhat in hardhat.config.ts
  2. Create a new script file to run at start.js with the code:
async function start() {
 const { run } = require("hardhat");
 // deploy your contact here
 await run("node");
 await new Promise(() => setTimeout(()=>{}, 2147483647)); // until ctrl+c
start() // + then/catch
  1. Run it with npx hardhat run start.js --network hardhat
  • Thanks for the answer, you are all correct and I figured it out, it's a matter of connecting to the spun up 8485 network!
    – blitter
    Jan 20 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.