This is Nestjs way how to set environment variables:
npm install @nestjs/config
That packages internally uses dotenv
package which puts together all environment variables in your machine.
app.module.ts
// configModule chooses the .env file, configservice extract the settings
import { ConfigModule, ConfigService } from '@nestjs/config';
@Module({
imports: [
// list your project's modules
ConfigModule.forRoot({
// this is set so we do not have to reimport the ConfigModule all over the place into other modules
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
// Notice we are not using TypeOrmModule.forRoot({})
// we set this to get access to ConfigService through dependency injection system
TypeOrmModule.forRootAsync({
// this tell DI system, find the configService which has all of the config info
inject: [ConfigService],
useFactory: (config: ConfigService) => {
return {
type: 'sqlite',
database: config.get<string>('DATABASE'),
synchronize: true,
entities: [User, Report],
};
},
}),