19

I am trying to use env variables in my html markup to change env variables according to whether I am in production or development mode .. So for context using mixpanel I have two projects one for development and one for production with different api keys. how would I use webpack to do this, accessing my process.env.VUE_APP_MIXPANEL env variable in my html ?

4
  • To answer this it is important to know how you are you serving your Vue project Commented Sep 18, 2018 at 9:01
  • @Imre_G I am using vue ui, and then running this command vue-cli-service serve --open . So you cannot use process.env in html script tags if I am not mistaken Commented Sep 18, 2018 at 9:13
  • I think you are correct. Maybe you can check this out: github.com/Glovo/vue-multianalytics#mixpanel Commented Sep 18, 2018 at 9:27
  • @Imre_G thank you .. Commented Sep 18, 2018 at 9:31

4 Answers 4

22

If you are using the default Webpack template you can access the .env variables in index.html using this syntax (for example):

<html>
  <head>
    <title><%= VUE_APP_TITLE %></title>
  </head>
  <body>
    ...
  </body>
</html>

Obviously you need to have a variable like this

VUE_APP_TITLE=My title

in your .env file

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

2 Comments

I tried but not working.
The default vue-webpack config seems to only utilize /.env properties that start with the string, "VUE_APP_".
4

With Google reCAPTCHA, I included my script like this:

<script src="https://www.google.com/recaptcha/api.js?render=<%= process.env.VUE_APP_RECAPTCHA_SITE_KEY %>"></script>

The env var is VUE_RECAPTCHA_SITE_KEY. It may be present in .env file.

3 Comments

I tried but not working.
does this work for variables in .env.production?
Yes, .env.production is only a way to define env var for that specific environment. More info: create-react-app.dev/docs/adding-custom-environment-variables/…
3

When using .env in index.html as HTML you can use this:

<link
  rel="icon"
  type="image/ico"
  href="<%= process.env.FAVICON_ICO_URL %>"
/>

or this:

<title><%= process.env.SITE_TITLE %></title>

When using .env variables injected into tags of index.html you need to use them as a string like this:

<!-- Google Tag Manager -->
<script>
  (function (w, d, s, l, i) {
    w[l] = w[l] || [];
    w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
    var f = d.getElementsByTagName(s)[0],
      j = d.createElement(s),
      dl = l != 'dataLayer' ? '&l=' + l : '';
    j.async = true;
    j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
    f.parentNode.insertBefore(j, f);
  })(
    window,
    document,
    'script',
    'dataLayer',
    '<%= process.env.GTM_CONTAINER_ID %>'
  );
</script>
<!-- End Google Tag Manager -->

Comments

0

To answer my own question, I then came across this package that allows me you to add google analytics to your vue projects .. https://github.com/MatteoGabriele/vue-analytics or https://github.com/Glovo/vue-multianalytics if you also want to add other tracking providers .

Comments

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.