1

I create an project with Typescript and NestJS, but i must run this project in vscode(from launch.json or terminal or something else) with this arguments from process.env:

"MYSQL_HOST":"x", 
"MYSQL_PORT":"x", 
"MYSQL_USERNAME":"x", 
"MYSQL_PASSWORD":"x", 
"MYSQL_DATABASE":"x", 
"NODE_ENV":"x"

can somebody tell me in which way i can do this?

2 Answers2

0

Add env variables to .env and import it using EasyconfigModule

import  { Module }  from  '@nestjs/common';
import { EasyconfigModule } from  'nestjs-easyconfig';

@Module({
 imports:  [EasyconfigModule.register({path: './config/.env'})],
})
export  class  AppModule  {}

Jacek Rojek
  • 1,082
  • 8
  • 16
  • nope, i do not want to do this in this way, i want to run this from terminal/cmd –  Jun 30 '20 at 16:43
  • @comodo assuming its for development you can set in in launch.json: https://stackoverflow.com/questions/48595446/is-there-any-way-to-set-environment-variables-in-visual-studio-code – Jacek Rojek Jun 30 '20 at 17:08
0

If you want to run NestJS from the terminal/cmd you can set environment variables using the npm package cross-env and then construct your command like this:

cross-env MYSQL_HOST=localhost MYSQL_PORT=1234 [and so on] nest start

The cross-env package isn't strictly necessary, it just makes it easier to have your command work across different environments. That way, you wont have to create separate commands for i.e. Windows and Linux.

Alternatively, you can use launch.json by setting up your variables in the env-property:

"configurations": [
    {
        ...
        "env": {
            "MYSQL_HOST": "localhost",
            "MYSQL_PORT": 1234
        }
    }
]
Nikolaj Dam Larsen
  • 5,455
  • 4
  • 32
  • 45