I am currently working on a nuxt project which needs to be deployed to multiple environments, such as stage, production-location-a, production-location-b.
Thus I made a function to load varies properties of each environment, and distinguish them via process.env.NODE_ENV.
const properties = env => {
let property = {}
switch (env) {
case 'development':
property = Dev
break
case 'productionLocationA':
property = ProductionLocationA
break
case 'productionLocationB':
property = ProductionLocationB
break
case 'stage':
property = Stage
break
default:
throw new Error(`Invalid NODE_ENV : ${env}.`)
}
return property
}
And start my project via below command in package.json:
{
"scripts": {
"dev": "nodemon --watch ... --exec \"nuxt\"",
"build": "nuxt build",
"start": "nuxt build && nuxt start",
...
"START:STAGE": "set NODE_ENV=stage && npm run start",
"START:PRODUCTIONA": "set NODE_ENV=productionLocationA && npm run start",
"START:PRODUCTIONB": "set NODE_ENV=productionLocationB && npm run start",
},
}
The process.env.NODE_ENV does is set to wanted value during building process, however is overwritten to production after executing nuxt start.
There are several hacking approaches like mentioned in #2741, or use nuxt-env etc. But is it possible for nuxt to support this officially ?
@LiangNex We have to consider that NODE_ENV is also internally being used by 3rd party modules (Including Vue.js runtime) to check against production value too. Also staging env is usually a production build but not running on official domain/host. So what I suggest you if you are not going to intentionally change the behavior, is simply using another env like NUXT_ENV like this:
env:{
NUXT_ENV: process.env.NUXT_ENV || process.env.NODE_ENV || 'development'
}
馃憤 thanks a lot for your advice. Really appreciate it.
Most helpful comment
@LiangNex We have to consider that
NODE_ENVis also internally being used by 3rd party modules (Including Vue.js runtime) to check againstproductionvalue too. Also staging env is usually a production build but not running on official domain/host. So what I suggest you if you are not going to intentionally change the behavior, is simply using another env likeNUXT_ENVlike this: