How to get environment variables in the aAPI side from the UI?

DK4213 注册会员
2023-01-24 19:39

Technically, we can't use environment variables in browser context, that's why we usually use DefinePlugin or EnvironmentPlugin in webpack based projects like CRA and Vue-CLI to statically replace process.env.* with environment variables.

But this way will forces us to multiple times for different stages.

To fix this, ., I recommend trying import-meta-env.

During production, this plugin statically replaces import.meta.env.* with placeholders (we use import.meta because process.env is a Node specific object), then we can run the built-in script and replace the placeholders with real environment variables later.

I also created an example for Docker. For Jenkins, I think it can be done in the same way.

Hope this helps someone who needs it.

1234567891 未验证用户
2023-01-24 19:39

If you are using create-react-app you can define your env variables in the .env file, just prefix them with REACT_APP_.

If not, you can use build tools like webpack to add them for you

About the Author

Question Info

Publish Time
2023-01-24 19:39
Update Time
2023-01-24 19:39