How check if Vue is in development mode?

2020-06-30 05:19发布

When I run my Vue app, the console shows:

You are running Vue in development mode.
Make sure to turn on production mode when deploying for production.
See more tips at https://vuejs.org/guide/deployment.html

So now I want to check if Vue is in development from inside my templates by using:

console.log("mode is " + process.env.NODE_ENV)

But that only logs undefined Is there a different way to find the NODE_ENV in Vue?

My webpack config has this part:

if (process.env.NODE_ENV === 'production') {
  module.exports.devtool = '#source-map'
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: '"production"'
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    })
  ])
}

Perhaps relevant: I use typescript, so I included this type declaration:

declare var process: {
    env: {
        NODE_ENV: string
    }
}

9条回答
戒情不戒烟
2楼-- · 2020-06-30 05:26

If you started with vue-cli (default webpack) then this should work:

  connection: process.env.NODE_ENV === 'development'
    ? 'ws://localhost:5000'
    : 'wss://myawsomeproject.org'
查看更多
聊天终结者
3楼-- · 2020-06-30 05:27

Webpack is used for almost all of my Vue projects, so I check to see if webpackHotUpdate is present.

 if (webpackHotUpdate) {
      console.log('In Dev Mode');
 }

It's present in the window object if the webpack dev server is running.

查看更多
三岁会撩人
4楼-- · 2020-06-30 05:30

Using .env file is a common way to set environmental variables used in a lot of stacks. It makes sense to use it in Vue, not to try to reinvent the wheel.

Here's a little test, which will show what conditions and options you have.

Build your project this this command:

vue-cli-service build

.env file:

#.env
NODE_ENV=development
DDD=development
VUE_APP_NODE_ENV=development

Vue component:

mounted() {
    console.log(process.env.NODE_ENV); // OUTPUT: production
    console.log(process.env.DDD); // OUTPUT: undefined
    console.log(process.env.VUE_APP_NODE_ENV); // OUTPUT: development
},

NODE_ENV is set by vue-cli-service. You can have multiple .env files and use vue-cli-service build --mode staging to run different configurations.

There are environment variables used during build and client-side env variables used in the component code. So you cannot use something like DDD in your client-side code, because Vue will ignore it.

You can create your own env variable prefixed with VUE_APP_ and use them in your client-side code for any checks. Docs ref. VUE_APP_NODE_ENV will work fine in our test.

NOTE

Parsing your url is not the best choice. If you use somethings like this window.location.href.indexOf("localhost"), it will not work for 127.0.0.1. There were a few times I had to run the project on a FQDN, and this check will not work for it eaither.

查看更多
Juvenile、少年°
5楼-- · 2020-06-30 05:31

For my particular case where I use pug and just wanted to conditionally add some elements to a component I set the options.data prop of pug-plain-loader in my webpack.config.js such that the loader looks like the following:

{
  resourceQuery: /^\?vue/,
  use: [
    {
      loader: 'pug-plain-loader',
      options: {
          // Use whatever you'd use to detect mode in the webpack config
          data: { mode: process.env['PRODUCTION'] ? 'production' : 'development' },
        },
      },
    ],
  },
}

Here's the full webpack.config.js I'm using: https://github.com/SuperuserLabs/thankful/blob/5913d9d0bb02e6d2f3b88c541477dc557caa4148/webpack.config.js#L76-L88

After which I could do:

if mode === 'development'
  | Only shown in development mode

For the general case, this was harder than I first anticipated. Although someone good at Webpack could probably do this pretty easily.

查看更多
祖国的老花朵
6楼-- · 2020-06-30 05:33

Absolutely the most simple solution is to check for the window.location from you Vue component. That would look something like this:

if (window.location.href === 'YOUR DEVELOPMENT URL') {
    //preset form values here
}
查看更多
祖国的老花朵
7楼-- · 2020-06-30 05:35

I know this is an old question but it may be helpful to new VueJS users to know this solution that I found in the current version of Vue (3.11):

When running in dev mode the property Vue.config.devtools is true, in production mode it is false!

查看更多
登录 后发表回答