Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PORT env var is not respected when set in .env file #32603

Closed
mbohgard opened this issue Dec 17, 2021 · 10 comments · May be fixed by #44392
Closed

PORT env var is not respected when set in .env file #32603

mbohgard opened this issue Dec 17, 2021 · 10 comments · May be fixed by #44392
Assignees

Comments

@mbohgard
Copy link

What version of Next.js are you using?

12.0.7

What version of Node.js are you using?

14.17.6

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

N/A

Describe the Bug

Setting PORT=4000 for ex is not respected when set in .env, .env-development etc .env files.
Loggin out process.env.PORT in next.config.js is reporting the correct port (4000) but the server is still starting on port 3000 (default).
The behaviour is the same with next dev and next start.

Expected Behavior

The server should use PORT from .env files as when started with PORT=4000 next dev.

To Reproduce

  1. Have a .env file in the root of the Next project with the content PORT=4000.
  2. Start the Next dev server with next dev.
  3. Server is starting on port 3000.
@mbohgard mbohgard added the bug Issue was opened via the bug report template. label Dec 17, 2021
@timneutkens
Copy link
Member

This is actually not a bug, booting the http server asap has the highest priority in next dev / next start so it happens before loading next.config.js / dotenv files. Looks like the documentation is not clear on that so we'll document it 👍

@timneutkens timneutkens added area: documentation and removed bug Issue was opened via the bug report template. labels Dec 17, 2021
@mbohgard
Copy link
Author

Ok, yeah, especially since you have a .env file example with PORT=8080 in it 🥴 Thanks for the quick response!

@timneutkens
Copy link
Member

That example is constructing an external API url. But can be changed

@kachkaev
Copy link
Contributor

kachkaev commented Feb 11, 2022

Reading all .env* files should take less than few milliseconds, which should not be noticeable both in next dev and next start.

start server
read .env*
read next.config.js

read .env*
start server
read next.config.js

I just ran PORT=4242 yarn dev to avoid clashes with another server. It worked, so I moved PORT=4242 to .env.local as I typically do for other vars. The server launched at 3000, so I got confused and ended up here 🙂

What’s the practical benefit of launching the server before loading .env? How many milliseconds do we save?

@timneutkens
Copy link
Member

Reading all .env* files should take less than few milliseconds, which should not be noticeable both in next dev and next start.

With normal file reading in Node.js we already see it taking upward of 10ms+ per-file depending on how much is going on in the main thread, so it'll significantly slow down bootup.

@LSafer
Copy link

LSafer commented May 9, 2022

Will, I don't care about 1000ms so would you please add an option for those like me who don't care 😕

Also, I don't think anybody would care about 1000ms in ((production))

For development you could cache it or something like that or make it a production thing

@LSafer
Copy link

LSafer commented May 9, 2022

Sorry for the tuff language but I am not a native speaker and I don't know how to deliver my thoughts better than that 😅

@SCG82
Copy link
Contributor

SCG82 commented Jan 22, 2023

Reading all .env* files should take less than few milliseconds, which should not be noticeable both in next dev and next start.

With normal file reading in Node.js we already see it taking upward of 10ms+ per-file depending on how much is going on in the main thread, so it'll significantly slow down bootup.

@timneutkens How about adding an option to read .env before starting? #44392

@manovotny
Copy link
Contributor

Good callout,@mbohgard. I've made a PR (#51063) for removing PORT=8080 from an .env example to avoid any confusion.

The documentation regarding port customization has already been updated with a note that using .env is not currently possible, as seen below.

CleanShot 2023-06-09 at 14 10 18@2x

kodiakhq bot pushed a commit that referenced this issue Jun 10, 2023
This is to [avoid confusion](#32603 (comment)) about being able to set Next.js's port via `.env`, which is currently not allowed.
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants