I have been developing a rest API using NestJS with typescript. I have followed nestjs documentation on configuration and tried all sorts of tricks to inject env vars from .env.${NODE_ENV}
files. But each time i am hit with undefined
as the result. I have exhausted all ideas. Now I am looking towards the community for help. Please help me to identify where am i making a mistake.
tsconfig.ts
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noUnusedLocals": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./"
},
"exclude": ["node_modules", "dist"]
}
config.ts
import { Logger } from '@nestjs/common';
import { config } from 'dotenv';
import path = require('path');
const envpath = path.join(__dirname, 'envs', `.env.${process.env.NODE_ENV}`)
Logger.log(`using profile: ${process.env.NODE_ENV} & config path ${envpath}`);
config({ path: envpath });
const APPCONFIG = {
ENABLE_CORS: process.env.ENABLE_CORS === 'true',
SWAGGER_SCHEME: (process.env.SWAGGER_SCHEME as 'http' | 'https') || 'https',
APP_VERSION: process.env.APP_VERSION,
DEPENDENT_SERVICE_BASE_URL: process.env.DEPENDENT_SERVICE_BASE_URL,
};
export { APPCONFIG };
app.module.ts
import { Logger, Module } from '@nestjs/common';
import { AuthModule } from './abcd/authentication/auth.module';
import { HealthModule } from './health-api/health.module';
import { ConfigModule } from '@nestjs/config';
import path = require('path');
const envpath = path.join(__dirname, 'envs', `.env.${process.env.NODE_ENV}`)
Logger.log(`using path for configService : ${envpath}`);
@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true, envFilePath: `${envpath}` }),
AuthModule,
HealthModule
]
})
export class AppModule { }
auth.module.ts
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
@Module({
imports: [],
controllers: [AuthController],
providers: [AuthService],
exports: [AuthService]
})
export class AuthModule { }
auth.service.ts
import { ConfigService } from '@nestjs/config';
@Injectable()
export class AuthService {
constructor(private readonly configService: ConfigService)
async timestamp(timestampDto: TimestampDto, timeStampUrl: string, authorization: string): Promise<any> {
try {
const token = this.extractBearerToken(authorization);
this.logger.log(token);
const downstreamAPIUrl = this.ConfigService.get(TIMESTAMP_URL);
console.log('API URL: ' +downstreamAPIUrl); #returning undefined
const headers = {
Authorization: `Bearer ${token}`
};
this.logger.log(timestampDto.payload);
this.logger.log(timestampDto.publicCert);
const formData = new FormData();
formData.append('payload', timestampDto.payload);
formData.append('publicCert', timestampDto.publicCert);
headers['timestampUrl'] = timeStampUrl;
const response = await axios.post(downstreamAPIUrl, formData, { headers });
return response.data;
} ...
I have .env.{NODE_ENV}
files in envs folder. the below is my project structure.
this is my directory structure...
├───dependencies
│ └───interface
├───abcd
│ └───authentication
│ └───dto
│ └───interface
├───envs
├───exceptions
│ ├───filters
│ └───persistors
├───health-api
│ └───interface
├───launchdarklyConfig
│ └───__mocks__
├───logger
this is inside src/production/abcd/app
path.
My .env.${NODE_ENV}
file contains
NODE_ENV=prod
TIMESTAMP_URL=https://abcde.com/api/timestamp
I have also tried not to use dotenv
as @nestjs/config
is wrapper around dotenv
package. As per their documentation, ConfigModule
and ConfigService
must be leveraged to fetch the values.
My regards to everyone in advance.
I created this minimal reproduce which works for me. You might want to compare your code to mine, it seems quite identical though (assuming you have typos in your code). Note that I just used
nest new .
to create this project from 0. Then just added theConfigModule
to the code.package.json
file:tsconfig.json file
:Some dummy environment variable file,
.yazif.env
:main.ts
file:app.module.ts
file:app.service.ts
file:app.controller.ts
file:Then, after doing on terminal
curl http://localhost:3000
I can see on the terminal log the environment variable indeed set:https://abcde.com/api/timestamp