title: secretOrPrivateKey must have a value - NestJs
body: <p>I get this error while implementing jwt in my server. I'm using NestJs and I'm following the documentation. I've been looking for solutions about this error here but it seems nothing works.
  I've stored the secret key in my .env file, add ConfigModule.forRoot() in my app.module, so I can successfully read all my environment variables. This is my code, if someone can point me in the right direction.</p>
  <p>app.module.ts</p>
  <pre><code>@Module({
    imports: [
      ConfigModule.forRoot(),
      TypeOrmModule.forRoot({
        type: 'postgres',
        host: process.env.DB_HOST,
        port: +process.env.DB_PORT,
        username: process.env.DB_USER,
        password: process.env.DB_PASS,
        database: process.env.DB_NAME,
        entities: ['dist/**/*.entity{.ts,.js}'],
        synchronize: false,
      }),
      UsersModule,
      AuthModule,
      PolsModule,
    ],
    controllers: [UsersController, AuthController, PolsController],
    providers: [UsersService, AuthService, PolsService, JwtService],
  })
  export class AppModule {}
  </code></pre>
  <p>auth.module.ts</p>
  <pre><code>@Module({
    imports: [
      UsersModule,
      PassportModule,
      JwtModule.register({
        secret: process.env.JWT_SECRET_KEY,
        signOptions: { expiresIn: '4h' },
      }),
    ],
    providers: [AuthService, UsersService, LocalStrategy, JwtStrategy],
    controllers: [AuthController],
  })
  export class AuthModule {}
  </code></pre>
  <p>auth.controller.ts</p>
  <pre><code>@Controller('auth')
  export class AuthController {
    constructor(
      private authService: AuthService,
      private usersService: UsersService,
    ) {}
  
    @UseGuards(LocalAuthGuard)
    @Post()
    async login(@Request() req) {
      return this.authService.login(req.user);
    }
  }
  </code></pre>
  <p>auth.service.ts</p>
  <pre><code>@Injectable()
  export class AuthService {
    constructor(
      private usersService: UsersService,
      private jwtService: JwtService,
    ) {}
  
    async login(user: any) {
      const payload = { username: user.username, sub: user.id };
      console.log(process.env.JWT_SECRET_KEY);
      return {
        access_token: this.jwtService.sign(payload),
      };
    }
  }
  </code></pre>
  <p>console error</p>
  <pre><code>// console.log(process.env.JWT_SECRET_KEY)
  fb8C47vylwS
  [Nest] 50225  - 01/07/2022, 17:46:37   ERROR [ExceptionsHandler] secretOrPrivateKey must have a value
  Error: secretOrPrivateKey must have a value
      at Object.module.exports [as sign] (/Users/user/dev/backend/pol-api/node_modules/jsonwebtoken/sign.js:107:20)
      at JwtService.sign (/Users/user/dev/backend/pol-api/node_modules/@nestjs/jwt/dist/jwt.service.js:28:20)
      at AuthService.login (/Users/user/dev/backend/pol-api/src/auth/auth.service.ts:26:37)
      at AuthController.login (/Users/user/dev/backend/pol-api/src/auth/auth.controller.ts:29:29)
      at /Users/user/dev/backend/pol-api/node_modules/@nestjs/core/router/router-execution-context.js:38:29
      at processTicksAndRejections (internal/process/task_queues.js:95:5)
      at /Users/user/dev/backend/pol-api/node_modules/@nestjs/core/router/router-execution-context.js:46:28
      at /Users/user/dev/backend/pol-api/node_modules/@nestjs/core/router/router-proxy.js:9:17
  </code></pre>
  <p>As I said I tried different potential solutions I found in other similar questions, (like <a href="https://stackoverflow.com/questions/58673430/error-secretorprivatekey-must-have-a-value">this one</a>) but none of the answers worked for me.</p>