Next.js Discord

Discord Forum

Build into a different directory

Answered
Blue orchard bee posted this in #help-forum
Open in Discord
Blue orchard beeOP
So I wanto to run the build script while still running the app so, i want to run next build but specify the destination dir.
I have run next build -h and there seems to be no option for that.
In alternative, i could do with having next start running a different directory than .next

There is a third option, which i'm sure it works but it would be more of a hassle, which would be to have a second clone of the repo just for building and copying the directory to the clone where my app is running from... I would much rather one of the first two options

Any help would be appreciated.

Thank you
Answered by not-milo.tsx
export a function from your next.config.js file. in it you can check in which context the config is loaded and return different configurations for each one
View full answer

20 Replies

European sprat
I could be wrong but I don't think it's possible. I don't recall seeing any mention in the docs. I think they wouldn't want to allow this since it could make deployments more complicated

Why do you want to change it?
Blue orchard beeOP
because i'm not running the app on Vercel, i'm running it in a ubuntu vps and, because it was built by monkeys typing randomly into the keyboard it takes 5 minutes to build at which point the app is not being served
so, third option it is
next dev and next build both output to the same directory. you can only [change it from the configuration](https://nextjs.org/docs/app/api-reference/next-config-js/distDir) but you can't have two separate output dirs for them.
European sprat
Oh nice so you can change it
yup, but it's still the same for all Next commands
Blue orchard beeOP
well, it doesn't work for me, as i need to have next start serving from one dir, and next build into another
maybe i can do some magic where i switch the config file when i want to build
here's a 4th option that might work, thanks @not-milo.tsx
I have an even better solution for you
export a function from your next.config.js file. in it you can check in which context the config is loaded and return different configurations for each one
Answer
Blue orchard beeOP
@not-milo.tsx i'm all ears
@not-milo.tsx that's above perfect. it saves me so much work and doesn't feel like a hack at all
@Blue orchard bee <@548149742207631360> that's above perfect. it saves me so much work and doesn't feel like a hack at all
I just tried it out and it works like a charm. I had a dev server running from a .next-dev folder and a build outputting to .next ✌️
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')

module.exports = (phase, { defaultConfig }) => {
  if (phase === PHASE_DEVELOPMENT_SERVER) {
    return {
      distDir: '.next-dev',
    }
  }

  return {
    distDir: '.next',
  }
}
Blue orchard beeOP
i would try it out immediately if it wasn't so late, but i couldn't go to sleep without figuring this out for first thing in the morning
Blue orchard beeOP
@not-milo.tsx, this is my usecase and it works perfectly
with the postbuild script