javascript - Vue.js: Defining computed environment variables in vue.config.js (vue cli 3) - Stack Overflow

The documentation for Vue CLI 3 says here .html#using-env-variables-in-client-side-code:You can have pu

The documentation for Vue CLI 3 says here .html#using-env-variables-in-client-side-code:

You can have puted env vars in your vue.config.js file. They still need to be prefixed with VUE_APP_. This is useful for version info process.env.VUE_APP_VERSION = require('./package.json').version

This is exactly what I want to do. But I couldn't find out how to actually define the env var there in vue.config.js. I tried:

module.exports = {
    process.env.VUE_APP_VERSION: require("../package.json").version,
    ...
}

But it just produces an error:

ERROR  SyntaxError: Unexpected token .
    /Users/lhermann/htdocs/langify/frontend/vue.config.js:2
    process.env.VUE_APP_VERSION: require("../package.json").version,
           ^

Does anyone know?

The documentation for Vue CLI 3 says here https://cli.vuejs/guide/mode-and-env.html#using-env-variables-in-client-side-code:

You can have puted env vars in your vue.config.js file. They still need to be prefixed with VUE_APP_. This is useful for version info process.env.VUE_APP_VERSION = require('./package.json').version

This is exactly what I want to do. But I couldn't find out how to actually define the env var there in vue.config.js. I tried:

module.exports = {
    process.env.VUE_APP_VERSION: require("../package.json").version,
    ...
}

But it just produces an error:

ERROR  SyntaxError: Unexpected token .
    /Users/lhermann/htdocs/langify/frontend/vue.config.js:2
    process.env.VUE_APP_VERSION: require("../package.json").version,
           ^

Does anyone know?

Share Improve this question asked Nov 1, 2018 at 3:49 lhermannlhermann 4906 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The environment variables are not part of the config export, you just set them in the vue.config.js file, eg

process.env.VUE_APP_VERSION = require('./package.json').version

module.exports = {
  // other config, eg configureWebpack
}

I've raised a feature-request to get an example added to the docs ~ https://github./vuejs/vue-cli/issues/2864

Common Environment Variables:

According to Environment Variables and Modes documentation, you can specify env variables by placing .env files in your project root.

The variables will automatically be accessible under process.env.variableName in your project. Loaded variables are also available to all vue-cli-service mands, plugins and dependencies.

.env                # loaded in all cases
.env.local          # loaded in all cases, ignored by git
.env.[mode]         # only loaded in specified mode
.env.[mode].local   # only loaded in specified mode, ignored by git

Your .env file(s) should look like this:

VUE_APP_MY_ENV_VARIABLE=value
VUE_APP_ANOTHER_VARIABLE=value

Note that only variables that start with VUE_APP_ will be statically embedded into the client bundle with webpack.DefinePlugin.

Computed Environment Variables:

If you want variables that need pre-processing, you can use chainWebpack property of vue.config.js to inject anything you want:

// vue.config.js

module.exports = {
  // ...,
  chainWebpack: config => {
    config.plugin('define').tap(args => {
      args[0]['process.env'].APP_VERSION = `"${require("../package.json").version}"`
      return args
    })
  }
  // ...
}

Using this method, you can inject anything, with any names you want; you are not bound by the VUE_APP_ limitation.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743707558a4493618.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信