Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotenv not loading env variables with correct path

I'm using the dotenv library, but my env variables returns undefined

Here is app.ts:

require('dotenv').config({path: '/.env'});
console.log(process.env.MAIN_DB_PATH) // returns undefined

Here is my .env file:

MAIN_DB_PATH=./data/database.db
UPLOAD_MULTER_DIR=./module/car/uploads

My folder structure is

enter image description here

So it should works fine :(

like image 304
goplay Avatar asked Sep 11 '25 07:09

goplay


1 Answers

You do not need path if the .env file is at the root, but you can define a return value from config method and check if error happend

const result = dotenv.config()

if (result.error) {
  throw result.error
}

console.log(result.parsed)

source: https://www.npmjs.com/package/dotenv config paragraph

like image 200
Zavael Avatar answered Sep 13 '25 23:09

Zavael