How to configure environment variables with vue-cli 3?
Asked Answered
H

5

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?

Heatstroke answered 6/11, 2018 at 19:10 Comment(2)
Well you did everything right! I just tried in my PC. You just have to restart your development server!Baum
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.Virnelli
P
6

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

ctrl + c

then

npm run serve
Piccoloist answered 25/4, 2019 at 7:16 Comment(1)
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.Electroencephalograph
W
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.

Windowlight answered 27/2, 2019 at 10:0 Comment(1)
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.Scarcely
N
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.

Newburg answered 25/7, 2019 at 8:32 Comment(0)
F
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.

Ferrand answered 8/3, 2019 at 9:53 Comment(0)
W
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

Witness answered 30/10, 2019 at 20:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.