Search code examples
node.jstypescriptdotenv

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


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'})