
Security News
Package Maintainers Call for Improvements to GitHub’s New npm Security Plan
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
nestjs-easyconfig
Advanced tools
Nestjs-easyconfig loads configs from your .env
(Wraps dotenv module) ⚙️ 🔥
$ npm install nestjs-easyconfig
$ yarn add nestjs-easyconfig
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({ path: './config/.env' })],
})
export class AppModule {}
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({ path: './config/.env', safe: true })],
})
export class AppModule {}
By default safe is set to false. When safe is set to true
, the module compares the supplied env
file with the sample env file to find missing keys. If any keys which are in .env.sample but not in the evironment used, it is immediately reported in console.
Note
: To use this, a sample env file .env.sample
should be placed in the root dir
Other config include dotenv's configs like encoding (Default: utf8) and debug(Default: false)
import { Module } from '@nestjs/common';
import { EasyconfigModule } from 'nestjs-easyconfig';
@Module({
imports: [EasyconfigModule.register({})],
})
export class AppModule {}
In this case, you have to pass in the NODE_ENV value and the .env
file to read will be determined accordingly.
Loads environment variables from .env.[development|test|production][.local]
files
For example, NODE_ENV=dev will make the app read .env.dev
Note: The .env file also has to be in root folder
Regardless of how the EasyconfigModule
is imported into the app, you can get the variable values using the EasyconfigService
.
import { Controller, Get } from '@nestjs/common';
import { EasyconfigService } from 'nestjs-easyconfig';
@Controller('api')
export class AppController {
constructor (private config: EasyconfigService) {}
@Get()
findAll() {
return {
value: this.config.get('key')
};
}
}
Note: the
get
method will automatically cast environment variables
Example of type processing:
Imagine you have a configuration file at .env
with the following:
FOO=bar
BAZ=2
BEEP=false
BOOP=some,thing,that,goes,wow
# note how we use an asterisk here to turn off the parsing for this variable
BLEEP=false*
# note how we use an asterisk in the array to turn off parsing for an array key value
PING=ping,true*,2,100
# note a string between bacticks won't be parsed
PONG=`some,thing,that,goes,wow`
After using this plugin, the environment variables are parsed to their proper types.
To test it out, simply log the returned object in your console:
console.log(env);
And you'll see that it outputs the properly parsed variable types:
{
// String
FOO: 'bar',
// Number
BAZ: 2,
// Boolean
BEEP: false,
// Array
BOOP: [ 'some', 'thing', 'that', 'goes', 'wow' ],
// NOTE: this was not parsed due to the * asterisk override above
BLEEP: 'false',
// NOTE: only the `true*` above was opted out through the use of an asterisk
PING: [ 'ping', 'true', 2, 100 ],
// NOTE: this was not parsed because the string was between bacticks
PONG: 'some,thing,that,goes,wow'
}
If your configuration line ends in *
it will not be parsed by this package, which allows you to keep values as the String
variable type if needed. Also when you encapsulate a value between bacticks e.g. `value`, the value won't be parsed and it will return as a String
variable. This can be used in situations where you for example have a ',' inside your string and it should not be parsed as an array.
/**
* path to the file to load.
* If this is not passed, Easyconfig load the environment file based on
* the NODE_ENV with the naming convention of `.env.<NODE_ENV>`.
*
* For example, if the NODE_ENV is `production`, the file `.env.<NODE_ENV>` will load.
*/
path ? : string;
/**
* path of the file to check the keys against when safe option is set to true.
* Defaults to .env.sample
*/
sampleFilePath ? : string;
/**
* checks whether the used env file is missing some keys.
*
* For example, if the given `.env` file has the following content:
* ```
* VAR=true
* ```
*
* and the `.env.sample` file has the following:
* ```
* VAR=true
* VAR2=sample value
* ```
*
* the following error log will be printed:
* ```
* MissingEnvVarsError: [VAR2] were defined in .env.example but are not present in the environment:
* This may cause the app to misbehave.
* ```
*/
safe ? : boolean;
/**
* As the lib uses dotenv, You may turn on dotenv's logging to help debug why certain keys or
* values are not being set as you expect.
*
* default : false
*/
debug ? : boolean;
/**
* This turns on parse logs which help debug how keys are being parsed.
*
* default : false
*/
parseLog ? : boolean;
/**
* This option lets you specify the encoding of your file containing environment variables.
*
* ```
*/
encoding ? : string;
/**
* This option allows you to pass in a pre-defined logger instance.
* The logger must implement the NestJS LoggerService interface
*/
logger ? : LoggerService;
/**
* This option allows you assign the values to process.env . Defaults to true
*/
assignToProcessEnv ? : boolean;
/**
* This option allows you overide a value on process.env if its alreadt set . Defaults to false
*/
overrideProcessEnv ? : boolean;
In general, we follow the "fork-and-pull" Git workflow.
src/
and test/
folders and/or README.md
.dist/
folder are autogenerated when running tests (npm run build
) and need not to be changed manually.The package is MIT licensed.
Hey dude! Help me out for a couple of :beers:!
Thanks goes to these wonderful people (emoji key):
Dmitry Lukanin 🚇 ⚠️ 💻 | Brad 🚇 💻 | Andras Bacsai 🚇 | Eugen Istoc ⚠️ 💻 | Jay McDoniel ⚠️ 💻 | Rubin shrestha 🚇 ⚠️ 💻 | Aavash Khatri 🚇 ⚠️ 💻 |
This project follows the all-contributors specification. Contributions of any kind welcome!
FAQs
Add configs to nestjs with auto type parsing
The npm package nestjs-easyconfig receives a total of 330 weekly downloads. As such, nestjs-easyconfig popularity was classified as not popular.
We found that nestjs-easyconfig demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maintainers back GitHub’s npm security overhaul but raise concerns about CI/CD workflows, enterprise support, and token management.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.