Member-only story
Implementing Externally Defined Configuration Settings in NestJS
Step ahead fast, with NestJS environment configuration
You have probably spent some time working with the awesome NestJS framework and now it’s time to see how to get some externally defined values for your configuration settings like some key or global variables, e.g.:
- App listening port number
- API global route prefix
- JWT parameters (token secret, token expiration time, e.g. in seconds)
- Database connection parameters (type, host, port, username, password, database)
- etc.,
NestJS provides the necessary documentation for working with different environments (development, production, etc.) using external configuration files and environment variables. So, if you have already taken a look there, then the below provided cases, will give you a hand to start implementing your (relatively simple) project as well.
NB: It is supposed that you have already installed the @nestjs/config package for your project and it is also good if you have also installed the @nestjs/jwt package and started working with JSON Web Tokens.
So, below you can find 3 of the most simple and common cases to use in your implementation(s).
The basics
The .env.dev
environment configuration file with defined properties and their values (like key/value pairs)
The .env.dev
:
The scripts in the package.json
file
For instance, we can run the dev configuration in our terminal:
npm run start:dev