Netlify dev changes proxy port eveytime it runs

I am trying to send fetch requests to a netlify function with netlify dev and gatsby. I had this working before, but now netlify dev is changing the proxy port that the function runs on. I am setting the port to send my fetch request in http-proxy-middleware. So I don’t know how to set the proxied netlify port to the function itself. Why does it keep changing the port number from like 45689 to 49303 so on… Stuck here. Thanks ahead of time. Im running node v13.6.0 in the one that gives me random ports, and node v14.1.0 that gives me a consistent port #

I have one project where it proxys to localhost:34567 and another where it keeps changing randomly, which makes it impossible to work with.

Ok so I upgraded that project to v14.1.0 and netlify dev is behaving in a way that will work now. I don’t know if this was a change between versions, but how would I use it for old projects

1 Like

hey wispyco, which version are you using?

I was running netlify netlify-cli/2.51.0 which gave me issues and netlify-cli/2.50.0 seems to give me the static port

Hey @wispyco, thanks for opening this thread. If I understand correctly, you’re trying to access Netlify Functions running with netlify dev server. We changed the port behavior of Netlify functions port recently to reduce conflicts. Users are not supposed to connect to Netlify functions directly. You can access your functions on netlify dev port at /.netlify/functions/*.

For example, if your netlify dev server is running on port 8888 and have a functions “hello”. You will be able to access that function on http://localhost:8888/.netlify/functions/hello. If you want a consistent address for your netlify dev instance while running multiple netlify dev instances, you can specify --port flag.

netlify dev -p 8885 will run the server on port 8885 or fail if that port is not available. Please try that and let us know if that doesn’t fix your problem.

3 Likes

Makes total sense thanks for the response.

That’s really helpful. Can you clarify how this would look in netlify.toml? Would it look like this:

[dev]
 command = 'yarn workspace site develop'
 functionsPort = 8885
 port = 8000
 publish = 'site/public'
 targetPort = 8888

The names ‘port’ and ‘targetPort’, would they mean:
port = “netlify listening for gatsby on this port”
targetPort = “access it in your browser on this port: as in localhost:8888”

Hey @dns, functionsPort will be ignored, so, you can remove that. targetPort should be the port your Gatsby server is running on and port is the port you want to access netlify dev server on (8888 by default). In you case, I think you have those two reversed.
A lot of people get confused on these port options. We want to revisit them and make them easier to guess. Please provide feedback here: https://github.com/netlify/cli/issues/911

2 Likes

Hello @raeesbhatti,

There must be a way to configure a port for functions. Every time I run netlify dev, the netlify functions are served on a random port. (Why is “functionsPort” ignored?)

Use case: I use Cypress.io to test my gatsby site. Until today (when I updated netlify dev cli) I could ensure that my cypress.io tests could access my functions at https://localhost:34567.

Cypress appears to override things to some extent – so I’ve had to write my tests such that they use absolute urls to the function (rather than urls relative to my gatsby site). But now that the functionsPort is random, I have an extra step to update the functions url used throughout the tests. every time I open cypress (which can be dozens of times per day)


On the other hand, if you happen to know of a way to configure Cypress to respect the work netlify-dev is doing to present the functions on the same port as my gatsby instance… I’m all ears!

As I said before

If your netlify dev server is running on port 8888 and have a functions “hello”. You will be able to access that function on http://localhost:8888/.netlify/functions/hello.

You were accessing functions server directly before, which is an anti-pattern. The reason we moved to random ports for the functions server is that it prevented users to run two instances of netlify dev with functions.
You should be accessing the functions server through netlify dev server at http://localhost:<the "port" you specified>/.netlify/functions/<function-name> just like on your deployed website.
If you want a consistent address for your netlify dev instance while running multiple netlify dev instances, you can specify --port flag.

In your case, I would suggest running Netlify Dev at a fixed port i.e. netlify dev -p 8888. Then you can replace http://localhost:34567/ with http://localhost:8888/.netlify/functions/ and everything should work fine.

I’ll also add: I understand and agree that hard-coding the port into my cypress tests is undesirable. (an anti-pattern.)

I suggest it’s also an anti-pattern to redact a configurable option so your users can no longer configure it, then also produce random variations that aren’t programmatically discoverable. (It the randomized port discoverable somehow as the the netlify dev server is starting up or after it’s running?)

Clearly, the functionsPort was desirable in the past (https://github.com/netlify/cli/pull/525) and I’d argue there are plenty of use-cases still.

I do hope you’ll reconsider the usefulness of having ‘knowable’ functions port. (If not a configurable one, at least knowable and not random.)

Hi, @dns. Part of the issue is that no one has a way to guarantee a specific TCP port will be available for use. If it is already in use, a different TCP port will be used instead.

Our solution for this is to rely on only the port being used by netlify dev. Once we stop relying on specific ports to be used by the functions, there are benefits for those ports to be random instead.

Again, netlify dev will report the port it is listening on. Even that will change if the default port is unavailable and no port is specified. If you specify a port and that isn’t available netlify dev will exit with an error stating it cannot start for that reason:

$ netlify dev
◈ Netlify Dev ◈
◈ Injected build setting env var: NETLIFY_BUILD_LIFECYCLE_TRIAL
◈ Injected build setting env var: netlify_build_enabled
Error: Could not acquire required "port": 8888
    at module.exports.serverSettings (~/.nvm/versions/node/v10.16.0/lib/node_modules/netlify-cli/src/utils/detect-server.js:118:11)

Again, if no port was specified, netlify dev will not exit but it will use a random port also.

To summarize, guaranteeing of ports used can be complicated and that is why we recommend only counting on the port used by netlify dev itself. I’m not saying that there isn’t value in a predictable port number. I’m only sharing my understanding of why we do it the way we do it.

I appreciate the response. Thank you.