Accessing Env variables in Sage

There is a few API keys that I need access to in Sage and I am looking for the best way to store them. Ideally I would like to pull the variables in the .env file in Bedrock’s /Site so I can get access to the different environments.

I have added dotenv-webpack to webpack.config.js but haven’t been able to access the variables in main.js. Is there something I am missing?

This is going to provide your env values to your build process, not your scripts. If you want to access env vars in main.js you might try dotenv.

Thanks a lot for your help! This confirms my suspicion. I installed Dotenv quickly but was getting a “fs dependency was not found” and didn’t look further into it.

For anyone looking for an answer, here is what I used to access my .env variables through Webpack:
new webpack.DefinePlugin({ 'process.env': JSON.stringify(dotenv.parsed) })

This topic was automatically closed after 42 days. New replies are no longer allowed.