0
votes

I have an NextJS app that I build on a build server then deploy to another server to host it.

When I start the app in development mode npm wants to recompile the app even though all of the built components still exist. (.next folder, etc...)

When I run the app in a non development mode for next the app will start up with no build attempts.

Why does npm want to rebuild the app when const app = next({ true });?

In server/server.js

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });

In package.json

"scripts": {
    "dev": "NODE_ENV=development npm start",
    "staging": "NODE_ENV=staging npm start",
    "prod": "NODE_ENV=production npm start",
    "build": "next build",
    "start": "node server/server.js"
}

How I'm starting the app:

Development: npm run dev

Production: npm run prod

1

1 Answers

0
votes

For a lack of better description the .next folder is the "optimized" version of your application. In development you need to continuously rebuild the .next folder because this is where you are serving your files from. During development you can actually see the .next rebuilding itself. In production you should only build the application once.

I imagine if you're deploying in Now it will build the .next folder as long as you have scripts like they define in setup. However, I use Docker for my builds so I have to build my .next folder via a build step. Here is an example of a Dockerfile and a corresponding package.json.

Dockerfile

FROM node:10-alpine AS builder
WORKDIR /app
COPY ./app .  
RUN yarn install && yarn next-build
EXPOSE 80
CMD yarn start

package.json

...
"scripts": {
   "next-build": "next build",
   "start": "NODE_ENV=production node server/app.js",
...

So in production my build steps are 1. Install npm modules 2. Build my .next folder 3. Start my server.

In short: In production since you aren't running a "next-build" script before you run "npm run prod" you aren't rebuilding the .next folder. In development next.js rebuilds each time you start your server so it can capture your changes in the .next folder.