woda
woda

Reputation: 105

dotenv environment variables problem with typescript

My project structure look like this:

|-project
   |-src
     |-index.ts
   |-.env

in index.ts I'm trying to load environment variables:

import dotenv from 'dotenv';
dotenv.config();

I've also tried with

dotenv.config({
    path: __dirname+"/../.env"
});

file .env itself is surely ok, where is the mistake? Thank for help

command that I'm running is ts-node ./src/index.ts

edit: I'm accesing values using expression like process.env.DB_CONNECT

.env file

DB_CONNECT=someValue
TOKEN_KEY=someValue

Upvotes: -1

Views: 3043

Answers (3)

Zahiduzzaman Setu
Zahiduzzaman Setu

Reputation: 1

The dotenv people suggest using the import 'dotenv/config'; that way it includes and initiates the config before anything else. not sure if that is what you are looking for here

Upvotes: 0

woda
woda

Reputation: 105

nevermind, it was my bad implementation of default values in functions. closing

Upvotes: -1

Tuan Duc Vo
Tuan Duc Vo

Reputation: 54

Can you try this?

import { resolve } from 'path';
import dotenv from 'dotenv';
dotenv.config({ path: resolve(__dirname, "../.env") });

Upvotes: 1

Related Questions