How do I setup the dotenv file in Node.js?

前端 未结 30 1143

I am trying to use the dotenv NPM package and it is not working for me. I have a file config/config.js with the following content:



        
30条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 14:44

    I had the same problem and I tried 4 hours to find the fault. In my case, it was bizarre.

    When I tried "node app.js", it worked. When I wanted a daemon to start it, it did not work.

    How did I solve my problem? I replaced:

    var dotenv = require('dotenv');
    dotenv.load();
    

    with:

    var dotenv = require('dotenv').config({path: path.join(__dirname, '.env')})
    

提交回复
热议问题