Next.js Discord

Discord Forum

Keep output and dev files separate

Answered
yolocat posted this in #help-forum
Open in Discord
I need to build my Next application with output: "export" and distDir: "out/web/". This does work partially, it does build correctly, but the dev files (dev server cache, trace, etc) use this folder too, which messes up my build pipeline. Can I make these paths separate?

This is the contents of out/web/ when a dev server has been running before building:
.well-known
404.html
_next
account.html
account.txt
app-build-manifest.json
auth
build-manifest.json
cache
index.html
index.txt
package.json
react-loadable-manifest.json
server
static
test-images
trace
types

while this is a clean build:
.well-known
404.html
_next
account.html
account.txt
auth
index.html
index.txt
test-images

(I have a custom setup with builds so I need to have a custom distDir set)
Answered by yolocat
Solution is to add this check your config:
distDir: process.env.NODE_ENV === "production" ? "your/out/folder/" : undefined, // or change `undefined` to your preferred dev folder, or even more logic for multiple dev folders simultaneously ;)
View full answer

4 Replies

Just tried checking process.env.NODE_ENV inside of next.config.ts, and it does actually use development for next dev and production for next build, can I simply use that and only set distDir when process.env.NODE_ENV === "production"?
Welp, it seemed to actually work flawlessly..! My next.config.ts has now passed the "more-logic-than-config"-bar but it now does exactly what I want.
Solution is to add this check your config:
distDir: process.env.NODE_ENV === "production" ? "your/out/folder/" : undefined, // or change `undefined` to your preferred dev folder, or even more logic for multiple dev folders simultaneously ;)
Answer
(This is just what I found, there may be a better way to achieve this, but this is all I've got!)