My implementation of a CryptoModule
is quite straightforward:
import { Module } from '@nestjs/common';
import { CryptoService } from './crypto.service';
@Module({
providers: [CryptoService],
exports: [CryptoService],
})
export class CryptoModule {}
The CryptoService
within this module relies on an environment variable to determine the secret key, which is facilitated using the Nest Config package.
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import Cryptr from 'cryptr';
@Injectable()
export class CryptoService {
constructor(private readonly config: ConfigService, private cryptr: Cryptr) {
this.cryptr = new Cryptr(this.config.get('CRYPTO_SECRET'));
}
encrypt = this.cryptr.encrypt;
decrypt = this.cryptr.decrypt;
}
To incorporate the ConfigModule, it is included in the app.module
as shown below:
imports: [
ConfigModule.forRoot({
envFilePath: !ENV ? '.env' : `.env.${ENV}`,
isGlobal: true,
}),
However, I am encountering the following error message:
"Error: Nest can't resolve dependencies of the CryptoService (ConfigService, ?). Please make sure that the argument dependency at index [1] is available in the CryptoModule context.\n"
Despite having the ConfigModule
defined as global, it seems like it still needs to be imported into the crypto module. I attempted to do so but the error persists. Am I overlooking something?
Currently, the only usage of this module is seen here:
import { Module } from '@nestjs/common';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { CryptoModule } from '../crypto/crypto.module';
@Module({
imports: [CryptoModule],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
Additionally, the CryptoService
is utilized within the service where it is imported.