Search code examples
node.jsnpmdotenv

ENV variables not being loaded into build production


I have used dotenv module to load environment modules into a config file. When I execute that file using node config.js I know that it has been loaded successfully(used console.log and saw output on the terminal). But when I am trying to run the entire project, it comes out as undefined in the console of the browser. Can anyone have a particular reason as to why this is happening?


Solution

  • running node config.js would run the file for sure and env variable will be available only during the life of this script. that's the reason you are not getting env variable in other places. try to inject require('dotenv').config() in the right place where you are consuming them. e.g build script. hope it helps.