14

i try to use environment variables in vue-cli, but is not working, everytime says 'undefined' when use console.log(process.env.VUE_APP_BASE_URI). But process.env.NODE_ENV says 'development' as i defined in webpack.config.js.

I read the documentation of vue-cli 3 and put my .env.* files in the root project. like this: enter image description here

Webpack.config.js

    module.exports = {
  mode: 'development',
  context: __dirname,
  entry: {
    main: ['babel-polyfill', './App/index.js']
  },
  plugins:[
    new VueLoaderPlugin()
  ],

.env.development

VUE_APP_BASE_URI=http://localhost:64208/api/

.env.production

VUE_APP_BASE_URI=http://localhost:64208/api/

I use .NET Core with Vue. How to use the environment?

2
  • 1
    Well you did everything right! I just tried in my PC. You just have to restart your development server! Commented Nov 6, 2018 at 19:57
  • 3
    Vue cli 3 doesn't use webpack.config.js it uses vue.config.js - important distinction. You would set --mode development and --mode production in your scripts in package.json so the correct environment is loaded. Commented Nov 6, 2018 at 21:01

5 Answers 5

6

it just happened to me, the solution is very simple. Just restart your development server.

ctrl + c

then

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

1 Comment

you could also check your webpack.config.js by running vue inspect in your terminal or vue inspect > webpack.config.js to add file in project directory.
2

There might be different ways to achieve this, one easy way from command line would be:

npx vue-cli-service serve --mode production

Default to development if mode was not specified.

1 Comment

For whatever reason, inside a docker container I wasn't able to get the .env files to work via npm run serve --mode=production (I'm assuming it was a versioning issue somewhere), but this ended up getting the .env files to work.
1

You need to rename the file to .env only instead of .env.production or .env.development. it should work, if not, comment on the same thread.

Comments

0

I do not know if this is the same problem that you had, but this happend in my case:

  • I set up everything correct as you did (Not any .config.js file)
  • I then stopped and startet the dev-server with npm run serve
  • When I now refreshed the page, nothing changed in the console

Then I saw that the npm run serve command had started the site on a different port!. The old version was still running, but gave the old values. When I tried the new port the values where correct.

Comments

0

In my case (Vue CLI 3 with Core 2.2) I have to put the .env files under the Core webapp folder. It did not work if I have them in wwwroot or the Vue app folder.

enter image description here

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.