2

I build my production version of Vue client using webpack, and inside of my Vue code, I use some environment variables. The problem is that when it is building no env vars are set.

I tried following stuff.

new webpack.DefinePlugin({
    'process.env' : {
        MYVAR : JSON.stringify(process.env.MYVAR)
    }
})

However, since when it is being built, env var is not yet provided (it will be provided during the run time) and they become just undefined.

What I'd like to do is accessing env vars during run time, not setting them up when it is being built.

Summary: Assuming that I build it with webpack, is there any way I can use environment variables that is provided when the server starts to run, not when it is built?

Any advice would be appriciated! Thanks!

3
  • According to Environment variables in docs, all you need to do is save your variable in a .[environment.][local.]env file in your project (i.e: .development.env). Commented Jun 2, 2019 at 3:45
  • Possible duplicate of Using Environment Variables with Vue.js. Read note at the end of accepted answer if you're using Vue CLI 3.x Commented Jun 2, 2019 at 3:49
  • 2
    That does not apply to this case because I can't keep .env file for security reason. It will be built before the env vars are set and it should get env vars from the shell when the server starts to run. Commented Jun 2, 2019 at 5:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.