question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using env variables inside Vue

See original GitHub issue

Hi, and excellent work!

I’ve tried creating a “.env” file, and editing Webpack to include additional environmental variables, but both methods fail for similar reasons.

So while environmental variables in the “.env” file pass npm run watch I get errors in Chrome if I attempt to use them:

mounted () {
  console.log("Index", VUE_APP_VAR)
}

ReferenceError: VUE_APP_VAR is not defined

If I use them inside Webpack, it renders the variables, but then fails to recognise them as such (I was using them in Axios).

What would the correct method be?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
OctaneInteractivecommented, May 29, 2019

I had to use:

new Dotenv({
  path: './.env',
}),

Without the path it wouldn’t work.

Thanks for the assistance — much appreciated!

0reactions
OctaneInteractivecommented, May 30, 2019

Ah that’s weird, because by default it’s alrady ./.env 🤔

That’s what I assumed. Yes, weird.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Modes and Environment Variables - Vue CLI
When running vue-cli-service , environment variables are loaded from all corresponding files. If they don't contain a NODE_ENV variable, it will ...
Read more >
Using Environment Variables with Vue.js - Stack Overflow
Create two files in root folder (near by package.json) .env and . · Add variables to theese files with prefix VUE_APP_ eg: ...
Read more >
How to Use Environment Variables in Vue.js - Medium
The environment variables can be accessed anywhere in your Vue application through the global process.env object. Here I have added two ...
Read more >
How to Use .env Environment Variables in Vue | HackerNoon
Vue can only use variables that start with 'VUE_APP_' and 'Vue_API_KEY' Vue-CLS lets us use these variables when we run our applications. Using...
Read more >
How To Use Environment Variables in Vue.js - DigitalOcean
With Vue.js, it is possible to use environment variables through files with the .env file extension. These files are responsible for storing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found