Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gatsby error [HPM] Error occurred while trying to proxy request / from localhost:8000 to http://localhost:4000 (ECONNREFUSED)

Developing a Gatsby App using this Starter https://github.com/the-road-to-react-with-firebase/react-gatsby-firebase-authentication

I keep getting this HPM Error after updated my node packages when I try to visit my page after running Gatsby Develop. The project compiles successfully but then I get this error in the browser and nothing shows up.

error occurred while trying to proxy to: localhost:8000/

and this in the terminal:

error [HPM] Error occurred while trying to proxy request / from localhost:8000 to http://localhost:4000 (ECONNREFUSED

once I remove this from the gatsby-config.js file it works and the pages generated in the browser:

module.exports = {
    developMiddleware: app => {
        app.use(
            proxy({
                target: "http://localhost:4000",
            })
        )
    },
}

However, I then get this error in the terminal:

Error loading a result for the page query in "/404.html". The query was not run and no cached result was found. Page not found /404.html

I want to know why isn't the Proxy working and what is the above module exports really doing anyway. I feel like this workaround I'm doing isn't good. Any help or advice would be great!!

Github Repo:

GitHub Repo for The project

like image 857
Avery-Dante Hinds Avatar asked Jun 29 '19 19:06

Avery-Dante Hinds


2 Answers

That error means there's nothing running at http://localhost:4000. There seems to be a few problem with your setup:

First, your developMiddleware setup points to http://localhost:4000, but your server (server.js) by default runs at http://localhost:3000. Perhaps you forgot to startup the server, or start it up at the wrong port?

Second, if I read it correctly,in your proxy middleware, you're proxying every route to port 4000? This will render Gatsby useless. Here's an example of a better proxy setup:

module.exports = {
  developMiddleware: app => {
    app.use(
      "/api",
      proxy({
        target: "http://localhost:4000",
      })
    )
  },
}

With this, only request to localhost:8000/api will be proxied to localhost:4000.

Hope it helps!

like image 80
Derek Nguyen Avatar answered Oct 26 '22 23:10

Derek Nguyen


I did have same error ECONNREFUSED while trying to debug both Angular and Node.js with VS Code and WSL2 Ubuntu-20.04 environment. Apparently that was solved by adding deprecated useWSL flag

{
  "type": "node",
  "request": "launch",
  "name": "Launch Node Express via NPM",
  "runtimeExecutable": "npm",
  "useWSL": true,
  "runtimeArgs": ["run-script", "node:debug"],
  "port": 9229
},
like image 29
volody Avatar answered Oct 26 '22 22:10

volody