Replies: 13 comments 2 replies
-
This will also is important for dockerized environments. If you use docker there's no easy way to use env variables in |
Beta Was this translation helpful? Give feedback.
-
I was quite surprised that the documented |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
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: |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
This comment was marked as spam.
This comment was marked as spam.
-
@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 🍻 |
Beta Was this translation helpful? Give feedback.
-
See #11408 (comment). It's breaking so can only be added in a major version. |
Beta Was this translation helpful? Give feedback.
-
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/ |
Beta Was this translation helpful? Give feedback.
-
SOLUTION: // package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start -p ${PORT-3000}" // <-- This
}
} |
Beta Was this translation helpful? Give feedback.
-
FYI on latest |
Beta Was this translation helpful? Give feedback.
-
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.Beta Was this translation helpful? Give feedback.
All reactions