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.

Use process.env for PORT

See original GitHub issue

I’m submitting a…

  • Regression
  • Bug report
  • Feature request
  • Documentation issue or request
  • Support request

Current behavior

Starting a new project includes an anti-pattern concerning the handling of configuration. Specifically, src/main.ts has the app listen on a hardcoded port.

E.g.

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

On its own this is a seemingly small thing, but it elevates the risk of projects neglecting one of the tenants of The Twelve-Factor App:

Store config in the environment

_https://12factor.net/config_

A consumer of the framework would need to be aware of this anti-pattern and discover the documentation recommending how to properly handle configuration with Nest.

Expected behavior

Instead of the app listening to a hardcoded port, use a ConfigService class as suggested in the docs to pull the port from the environment.

Documentation should also link users to the page on configuration techniques.

Minimal reproduction of the problem with instructions

Start a new project with Nest’s CLI (nest new project) and see that src/main.ts has hardcoded port.

What is the motivation / use case for changing the behavior?

Two motivations for this feature request:

  1. Mitigate risk of applications being built on top of an anti-pattern.
  2. Educate developers who are newer to backend development by putting best practices in the critical path of using Nest.

Environment


@nestjs/cli: 5.8.0
@nestjs/common: 5.4.0
@nestjs/core: 5.4.0

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ryakovivcommented, Jul 6, 2022
export class AppModule {
   static port: string
  constructor(configService: ConfigService) {
    AppModule.port = configService.get('HTTP_PORT')
  }
}
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(AppModule.port || 3000);
}
bootstrap();
2reactions
felixhayashicommented, Nov 19, 2019

Actually, there is a popular npm package (https://github.com/lorenwest/node-config) that allows you to define config files for different “environments” and at runtime it will pick the config matching NODE_ENV. I think this sufficiently abstracts the configuration process and makes it fairly easy to switch environments (see the README examples). As @kamilmysliwiec noted, it is fine that this aspect of the configuration mechanism is not provided as a service since the bootstrap() is out of the DI-scope anyways. Of course, nonetheless, nothing prevents you from wrapping the config mechanism in a service to provide it to other parts of your app later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is process.env.PORT in Node.js?
If you are using this in windows machine, first you'll have to set the PORT variable as "set PORT=3300", then in the next...
Read more >
process.ProcessEnv.PORT JavaScript and Node.js code ...
How to use. PORT. function. in. ProcessEnv. Best JavaScript code snippets using process.ProcessEnv. ... function getRootUrl() { const port = process.env.
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 >
What is process.env.PORT in Node.js?
process.env is a reference to the system environment variables. PORT means the application expects an environment variable named “PORT” to be set.
Read more >
process.env: How to Use Environment Variables in Node
The process.env is a global variable injected by Node.js at runtime for your application to use, and it shows the state of the...
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