"TypeError: Cannot read property 'config' of undefined" in dotenv

Issue

I’m trying to get access to my .env file via dotenv module, but I’m getting the below error

dotenv.config();
       ^
TypeError: Cannot read property 'config' of undefined

I have dotenv installed in node_modules and package.json, my .env file is in the root folder, and I think everything is written correctly, but it doesn’t work for some reason. I have tried reinstalling the module, didn’t help

Solution

try the following :

require('dotenv').config({path: __dirname + '/.env'})

In Typescript

import * as dotenv from 'dotenv'
dotenv.config({path: __dirname + '/.env'})

Answered By – AbolfazlR

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published