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 invoke npm 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
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 precedence
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
const port = args['--port'] || process.env.PORT || 3000;
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.
This will also is important for dockerized environments. If you use docker there's no easy way to use env variables in CMD
directive. And production builds usually come without npm where you can pass that as part of script.
I was quite surprised that the documented .env
/.env.test
file PORT
settings are not respected by the command line. This seems a bit unintuitive/non-standard and would be great if it could get addressed to make the package.json file simpler and make it more configurable using .env*
vars
This seems a bit unintuitive/non-standard
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 process.env.PORT
. I don't think I'm too far off base making the claim supporting that is standard?
Sails.js allows setting PORT env to configure port:
https://sailsjs.com/documentation/concepts/configuration
Feathers allows setting env vars and then passing them into your app without flags:
https://docs.feathersjs.com/api/configuration.html#example
Keystone.js supports process.env.PORT:
https://v4.keystonejs.com/documentation/configuration/server-options/
Kraken.js allows PORT env:
https://github.com/krakenjs/kraken-js/issues/142
Adonis.JS:
https://adonisjs.com/docs/3.2/env
Nest.js appears to also support it without flag:
https://docs.nestjs.com/techniques/configuration
I would really like this to be done for $HOSTNAME
and $PORT
to respect good practices of Docker and Node described here : https://github.com/goldbergyoni/nodebestpractices/blob/master/sections/docker/bootstrap-using-node.md
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:
ARG HOSTNAME=0.0.0.0
ENV HOSTNAME=$HOSTNAME
ARG PORT=8080
ENV PORT=$PORT
EXPOSE ${PORT}
CMD ["/usr/local/bin/node", "node_modules/next/dist/bin/next", "--", "start", "-p", ${PORT}, "-H", ${HOSTNAME}]
Allowing to use the env vars would help to reduce the command to:
CMD ["/usr/local/bin/node", "node_modules/next/dist/bin/next", "--", "start"]
In the meanwhile, I'm stuck with npm start
馃し.
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 next start
.
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: https://github.com/vercel/next.js/pull/11408#issuecomment-617034254
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.
Sure. Below are some examples. I'll also add that pretty much every Node project I've interacted with in my professional career handles
process.env.PORT
. I don't think I'm too far off base making the claim supporting that is standard?Sails.js allows setting PORT env to configure port:
https://sailsjs.com/documentation/concepts/configurationFeathers allows setting env vars and then passing them into your app without flags:
https://docs.feathersjs.com/api/configuration.html#exampleKeystone.js supports process.env.PORT:
https://v4.keystonejs.com/documentation/configuration/server-options/Kraken.js allows PORT env:
krakenjs/kraken-js#142Adonis.JS:
https://adonisjs.com/docs/3.2/envNest.js appears to also support it without flag:
https://docs.nestjs.com/techniques/configuration
This is standard and if Next.js were to survive, there has to be support for allowing port configuration via .env setting
@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 https://github.com/vercel/next.js/pull/11408#issuecomment-610347795. It's breaking so can only be added in a major version.
Most helpful comment
Sure. Below are some examples. I'll also add that pretty much every Node project I've interacted with in my professional career handles
process.env.PORT
. I don't think I'm too far off base making the claim supporting that is standard?Sails.js allows setting PORT env to configure port:
https://sailsjs.com/documentation/concepts/configuration
Feathers allows setting env vars and then passing them into your app without flags:
https://docs.feathersjs.com/api/configuration.html#example
Keystone.js supports process.env.PORT:
https://v4.keystonejs.com/documentation/configuration/server-options/
Kraken.js allows PORT env:
https://github.com/krakenjs/kraken-js/issues/142
Adonis.JS:
https://adonisjs.com/docs/3.2/env
Nest.js appears to also support it without flag:
https://docs.nestjs.com/techniques/configuration