51

I am working on a Vue JS project. I made a .env file in the project's root directory. I made 3 variables. Here is the content of my .env:

API_BASE_URL=http://x.x.x.x:yyyy/myproject
VUE_APP_TITLE=My App
VUE_MY_VAR=My Var

I put the logs inside the component that loads on startup in beforeMount() method like this:

beforeMount() {
            console.log('base url: ' + process.env.API_BASE_URL);
            console.log('base url: ' + process.env.VUE_APP_TITLE);
            console.log('base url: ' + process.env.VUE_MY_VAR);
        }

When I run dev server using vue-cli-service serve I see the following console output:

base url: undefined
base url: My App
base url: undefined

My question is: why is the value of API_BASE_URL and VUE_MY_VAR undefined

1

1 Answer 1

110

From docs:

Note that only NODE_ENV, BASE_URL, and variables that start with VUE_APP_ will be statically embedded into the client bundle with webpack.DefinePlugin. It is to avoid accidentally exposing a private key on the machine that could have the same name.

So you need to add VUE_APP_ prefix to your variables.

Sign up to request clarification or add additional context in comments.

3 Comments

I'll also add that you may need to completely stop and restart your localhost so your app can pick up the changes.
Ah god. I was really frustrated as what really is wrong with my environment variable :)
Yep, very frustrating. Thanks for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.