Nuxt.js: Nuxt start accept NODE_ENV externally

Created on 18 Dec 2018  路  2Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

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 ?

This feature request is available on Nuxt community (#c8321)
feature-request

Most helpful comment

@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'
}

All 2 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bimohxh picture bimohxh  路  3Comments

msudgh picture msudgh  路  3Comments

VincentLoy picture VincentLoy  路  3Comments

vadimsg picture vadimsg  路  3Comments

gary149 picture gary149  路  3Comments