Warm tip: This article is reproduced from serverfault.com, please click

How to read environment vars for both python and javascript in Flask

发布于 2020-11-28 19:36:19

My app is to be deployed on Heroku. I set up environment variables in .env in my root directory.

Testing locally with Flask, I have Python accessing env vars using the dotenv package with no issues. I run into trouble when trying to access the environment variables with Javascript.

I read Node.js has it's own dotenv dependency to access environment variables in JavaScript using process.env.ENV_VAR. I installed the package into my virtual environment and started my js script with require('dotenv').config(), but when running the Flask app, the console returns:

Uncaught ReferenceError: require is not defined

This tells me I'm not accessing the node.js dependencies through Flask and gives me the suggestion it might not be possible.

Is there a way to access env vars through Javascript and Python for a Flask app?

Questioner
Joshua
Viewed
0
Joshua 2020-11-29 04:48:19

After some further researching, I came across

Everything that goes to client side doesn't belongs to you only. It is unrelated to Flask because it is server-side framework.

Turns out, looking into my API key I was trying to hide from the app is a Public Key that only provides read-access. There is no harm in publishing the key.