How to set port in code so that it passes Render's build?

I just tried my first deploy and got this error: ReferenceError: PORT is not defined

Here’s the code causing the error:
const port = process.env.port || PORT;

PORT is in my .env file, and it’s being used locally. I thought process.env.port would grab Render’s port, but apparently not.

So I added this to the Environment Variables UI:
key: PORT value: 5000

Then I got this error: const port = process.env.port || PORT; // (current: using .env) // with pointer to PORT

So then I added my .env file to the Render UI Secret Files like this:
Filename: .env Contents: code (including PORT=5000)

And now I’m back to this error: ReferenceError: PORT is not defined

So how exactly do I set the port in my code so that it works after Render’s build?

Thank you.

Hi @J_Buckley ,

It looks like your code is trying to interpret PORT as a variable, so it’s failing to evaluate that line. You’ll need to access environment variables through the process.env object and remove the standalone PORT.

Thank you @dan

I just tried this:

const port = process.env.port;

app.listen(port, (err) => {
if (err) {
console.log(There was a problem with app.listen: ${err});
}
console.log(Listening on port ${port});
});

And now I’m getting this log: Listening on port undefined
And the ‘In progress’ indicator hasn’t stopped.
When I try the url, I get a 502.

It looks like process.env.port is undefined. I’d recommend enumerating the environment variables available in process.env to see what is defined. It might be a case sensitivity thing.

1 Like

Case sensitivity was the issue.
This actually works: const port = process.env.PORT || 5000

Thank you!

1 Like