-
Notifications
You must be signed in to change notification settings - Fork 27k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
support $PORT environment var in next start
directly without passing it as argument
#10338
Comments
This will also is important for dockerized environments. If you use docker there's no easy way to use env variables in |
I was quite surprised that the documented |
If you're going to make the argument for "non-standard" it's always good to provide many examples of where this is the case. |
Sure. Below are some examples. I'll also add that pretty much every Node project I've interacted with in my professional career handles Sails.js allows setting PORT env to configure port: Feathers allows setting env vars and then passing them into your app without flags: Keystone.js supports process.env.PORT: Kraken.js allows PORT env: Adonis.JS: Nest.js appears to also support it without flag: |
I would really like this to be done for The fact is that using CMD do not allow to have dynamic arguments : https://stackoverflow.com/questions/40454470/how-can-i-use-a-variable-inside-a-dockerfile-cmd The following do not work:
Allowing to use the env vars would help to reduce the command to:
In the meanwhile, I'm stuck with |
I think this is also an issue when trying to deploy Next.js to Plesk environments. The official Node.js extension allows us to execute an npm command. However, I've no clue how to tell Next.js to use the port specified as the environment variable when using |
While I think the issue is still relevant, just in case you missed it like me, there is a clean workaround here to use in the meanwhile: #11408 (comment) I just adapted it that way to also pick HOSTNAME in the env: /* eslint-disable */
const cli = require('next/dist/cli/next-start');
cli.nextStart([
'-p', process.env.PORT || 3000,
'-H', process.env.HOSTNAME || '0.0.0.0',
]); That way it works well with Docker. |
This comment has been minimized.
This comment has been minimized.
@timneutkens I've offered a list of other frameworks that do this, any chance this could be considered as an enhancement to Next? I've found my own work arounds but it would be swell if it could be addressed at some point 🍻 |
See #11408 (comment). It's breaking so can only be added in a major version. |
ASP.NET Core does this very well IMHO, using an environment variable to specify bindings.
https://andrewlock.net/5-ways-to-set-the-urls-for-an-aspnetcore-app/ |
- Enables excludeDefaultMomentLocales by default - Adds distDir cleaning (See RFC #6009) - Adds support for `PORT` - Removes `router.events` from the server-side router as it should not be used server-side (long-standing todo that is potentially breaking). Note that it's still available as `Router.events` (import Router from 'next/router') and with `useRouter` in `useEffect`. Using it with `useEffect` is the correct way and I've updated the upgrading guide to reflect that - Added webpack 5 to the upgrading guide - Removed `Head.rewind` as it's been a no-op since Next.js 9.5 and can now be safely removed from user code Fixes #11408 Fixes #10338 Fixes #5554 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [ ] Make sure the linting passes
- Enables excludeDefaultMomentLocales by default - Adds distDir cleaning (See RFC vercel#6009) - Adds support for `PORT` - Removes `router.events` from the server-side router as it should not be used server-side (long-standing todo that is potentially breaking). Note that it's still available as `Router.events` (import Router from 'next/router') and with `useRouter` in `useEffect`. Using it with `useEffect` is the correct way and I've updated the upgrading guide to reflect that - Added webpack 5 to the upgrading guide - Removed `Head.rewind` as it's been a no-op since Next.js 9.5 and can now be safely removed from user code Fixes vercel#11408 Fixes vercel#10338 Fixes vercel#5554 ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [x] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. ## Documentation / Examples - [ ] Make sure the linting passes
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Feature request
Is your feature request related to a problem? Please describe.
We start to spawn more and more nextjs projects and we have ci/cd which allows us to deploy a new project with absolut minimal effort by using some conventions. E.g. we want the apps to run on production using the
start
script in package.json. So every node app should just invokenpm run start
.Many frameworks use some conventions which makes this approach easy, e.g. using $PORT as the default port to listen to if its defined. Unfortunatly, nextjs does not respect this environment variable. So we have to modify newly created nextjs projects and adjust the package.json.start script with:
next start --port $PORT
Describe the solution you'd like
next start
without arguments start the server listening to env var $PORT if its defined, otherways falls back to 3000. --port argument would still have precedenceDescribe alternatives you've considered
we could do more adjustments on our side, but supporting $PORT in nextjs would be a tiny change:
in https://github.com/zeit/next.js/blob/canary/packages/next/cli/next-start.ts#L48
Additional context
While this adjustment is extremly easy to do, it could have some impact on environments where PORT is already unexpectedly defined and no
--port
argument is used. i think this is a rare edge case. Most people who self host nextjs will have --port set, so it would not accidentially break for them.The text was updated successfully, but these errors were encountered: