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
}
}
If you started with vue-cli (default webpack) then this should work:
Webpack is used for almost all of my Vue projects, so I check to see if
webpackHotUpdate
is present.It's present in the
window
object if the webpack dev server is running.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:
.env file:
Vue component:
NODE_ENV is set by
vue-cli-service
. You can have multiple.env
files and usevue-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 for127.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.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 ofpug-plain-loader
in mywebpack.config.js
such that the loader looks like the following:Here's the full
webpack.config.js
I'm using: https://github.com/SuperuserLabs/thankful/blob/5913d9d0bb02e6d2f3b88c541477dc557caa4148/webpack.config.js#L76-L88After which I could do:
For the general case, this was harder than I first anticipated. Although someone good at Webpack could probably do this pretty easily.
Absolutely the most simple solution is to check for the window.location from you Vue component. That would look something like this:
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
istrue
, in production mode it isfalse
!