support $PORT environment var in `next start` directly without passing it as argument
See original GitHub issueFeature 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 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
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 precedence
Describe 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
const port = args['--port'] || process.env.PORT || 3000;
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.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:100
- Comments:11 (2 by maintainers)
Top GitHub Comments
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
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.