bjorn_h
bjorn_h

Reputation: 139

How can I make use of Vue .env settings in a .js file?

I have a .js file in which I would like to read the value of a Vue environment variable, as set via a .env.* file.

In my main.js file, where the Vue instance is created, I can read those variables/settings via "process.env. ...". However, in this other .js file, I cannot find a way to read them there. No "process.env. ..." variables are visible there. And I also cannot find them via the "this." object or the "Vue." object (whichever is set at that point in the code). E.g. not via something like "Vue.process.env. ...". I import Vue at the beginning of this file via:

 import Vue from 'vue';

but that doesn't help in any way, it seems.

How can I read these variables there?

Upvotes: 0

Views: 304

Answers (1)

Gabriele Di Simone
Gabriele Di Simone

Reputation: 156

process.env is accessible only in the node environment. If you want to use it in your client side code, you can use webpack.DefinePlugin; in Vue Cli 3, this is automatic for all variables that start with VUE_APP_: Env Variables Vue CLI 3

Upvotes: 2

Related Questions