Nuxt Use Different buildDir for Development and Production

February 17, 2020

Solution 1: dotenv

Setup dotenv.

Edit env.development.

BUILD_DIR=.nuxt-dev

Edit env.production.

BUILD_DIR=.nuxt

Edit nuxt.config.js.

export default {
  ...
  buildDir: process.env.BUILD_DIR,
  ...
}

Solution 2: process.env.NODE_ENV

export default {
  ...
  buildDir: process.env.NODE_ENV == 'production' ? '.nuxt' : '.nuxt-dev',
  ...
}

NOTE: When npm run dev is run, process.env.NODE_ENV == 'development'. When npm run build is run, process.env.NODE_ENV == 'production'

Solution 3: custom environment variables

export default {
  ...
  buildDir: process.env.BUILD_DIR
  ...
}

Edit package.json

{
  ...
  "scripts": {
    "dev": "env BUILD_DIR=.nuxt-dev nuxt",
    "build": "env BUILD_DIR=.nuxt nuxt build",
    ...
  }
  ...
}

NOTE: If you are using dotenv with the same environment variable specified, dotenv will overwrite the environment variable.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.