0

I have created .env file to set environment variables as shown below:

enter image description here

Inside which I have given below mentioned code.

VUE_APP_BASEURL="https://qa-apps-nodejs.Dtech.com:9000/"
PORT=8080

I have written below code in my js file and executed npm install dotenv:

require('dotenv').config();
console.log(process.env.VUE_APP_BASEURL);

With the above code, I need to get https://qa-apps-nodejs.Dtech.com:9000/ in console. But instead I'm getting:

undefined

4
  • are you using vue-cli ? Commented Feb 18, 2020 at 10:49
  • Yes. Vue CLI v4.1.2 Commented Feb 18, 2020 at 11:01
  • 2
    can you try solutions here. May be you can try restarting your server. Commented Feb 18, 2020 at 11:07
  • It worked now. I just restart the server. Thanks Commented Feb 18, 2020 at 11:13

2 Answers 2

5

Just restart your server as newly created env variables are not available only after hot-reload you have to rebuild your application (restart your server). For reference you can look here

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

Comments

1

No need to install "dotenv" simply place .env file in the root of your project (at the same level of your package.json file) and Vue CLI will handle the reset. Side note; as of Vue CLI 3 only variables that start with VUE_APP_ will be loaded. make sure also to rerun npm run serve again to restart your server.

for more details: visit docs

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.