Recon
Recon

Reputation: 618

Specify Next.js build directory in build command?

Originally, I had my distDir set to a folder within my Firebase Functions directory for deployment, however, the duplication of React when running the app in dev mode led to some errors, so I've had to comment it out.

Is there a way to specify the next build commands output directory within the command? Like next build dir='../functions/next'.

Or is there a way to separate dev builds directory (from the next command) from the production builds (next build) within the config?

Upvotes: 3

Views: 6829

Answers (2)

bukso
bukso

Reputation: 1366

I found following solution for this problem:

You can set custom environment variable that decides where your build will be generated:

BUILD_DIR=myBuilds/buildXYZ yarn build

OR

BUILD_DIR=myBuilds/buildXYZ npm build

In the next.config.js you can use your environment variable like this:

module.exports = {
  distDir: process.env.BUILD_DIR || '.next',
}

Now your build will be dynamically generated in the myBuilds/buildXYZ directory defined in the BUILD_DIR variable.

One thing to notice: If you want to use the next export command (or any other command using the build directory), you should also add the environment variable before it, e.g.:

BUILD_DIR=myBuilds/buildXYZ yarn export

Upvotes: 11

Atlante Avila
Atlante Avila

Reputation: 1488

According to https://github.com/zeit/next.js/issues/1513, this may need you to update to the next version. But it looks like they added a feature to next.config.js file. All you have to do is add the destination for your dist:

module.exports = {
  options: {
    dist: '.next'
  }
}

Haven't tested it, but it was successfully merged to master and marked as resolved.

Upvotes: 0

Related Questions