NestJs: Unable to read env variables in module files but able in service files?

Your .env file is not yet read in when your JwtModule is instantiated. So either read it in earlier e.g. in your main.ts before the nest app is created or better: create a ConfigService and make the dependency on your config explicit: JwtModule.registerAsync({ imports: [ConfigModule], useFactory: async (configService: ConfigService) => ({ secret: configService.jwtSecret, }), inject: … Read more

How to fix “File ‘…/node_modules/dotenv/types’ not found.” error coming from jsconfig.json?

I had the same issue. Restarting VSCode solved the problem. See: https://github.com/motdotla/dotenv/issues/475 Edit: this seems to be an issue with the contents of dotenv’s package.json. There’s an open PR, but the author hasn’t replied/merged yet at this time. Link: https://github.com/motdotla/dotenv/pull/476 Edit 2: This should now be fixed! dotenv v8.4.0 fixes this issue. See https://github.com/motdotla/dotenv/releases/tag/v8.4.0.

NestJS – How to use .env variables in main app module file for database connection

From Nestjs docs here – https://docs.nestjs.com/techniques/configuration These steps worked for me with MySQL and TypeORM. Install Nestjs config module – npm i –save @nestjs/config. It relies on dotenv Create a .env file in your root folder and add your key/value pairs e.g. DATABASE_USER=myusername Open app.module.ts and import the config module import { ConfigModule } from … Read more

How to add comments to .env file?

In 2022 both separate line comments and inline comments are supported. Line started with # symbol is a separate line comment. See the docs. Inlined # sign denotes the start of an inline comment (thanks to @reddisht to note this in comments). For vlucas/phpdotenv the same situation. Here is the example for both: # This … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)