question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

support $PORT environment var in `next start` directly without passing it as argument

See original GitHub issue

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 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:closed
  • Created 4 years ago
  • Reactions:100
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

32reactions
danawoodmancommented, Aug 12, 2020

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

16reactions
SkeLLLacommented, Mar 26, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Features: Environment Variables
Next.js comes with built-in support for environment variables, which allows you to do the following: Use .env.local to load environment variables ...
Read more >
Environment variables not working (Next.JS 9.4.4)
You need to add a next.config.js file in your project. Define env variables in that file and those will be available inside your...
Read more >
Learn How to Use Next.js Environment Variables ... - YouTube
In this video, you'll learn how to use and expose environment variables in Next.js. I cover how to use environment variables for both...
Read more >
Working with Environment Variables in Node.js
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
Read more >
Troubleshoot issues when passing environment variables ...
You can pass an environment variable inside your Amazon ECS task in ... Store the variable inside an AWS Systems Manager Parameter Store....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found