Deploy NextJS with Dokku in Production

Total
1
Shares

I have set up Dokku and want to deploy my basic NextJs to it. Everything works fine, except for that the application is running in development mode.

When I output the NODE_ENV variable in my JSX, it is first production but changes to development.

const Index = () => (
  <div>
    <Link href="/about">
      <a>About Page</a>
    </Link>
    {process.env.NODE_ENV}
  </div>

That’s what I am seeing. The NODE_ENV variable changes during the page load.

enter image description here

package.json:

"scripts": {
  "start": "next src",
  "build": "next build src"
},

App.json:

{
  "scripts": {
    "dokku": {
      "predeploy": "npm run build"
    }
  }
}

Procfile:

web: npm start -- --port $PORT

In addition I set two configs for my dokku application:

dokku config:set my-app NPM_CONFIG_PRODUCTION=false
dokku config:set my-app HOST=0.0.0.0 NODE_ENV=production

What am I missing to get it into production mode?


Solution

Solved it by setting up an own express server.

package.json

"scripts": {
  "dev": "node server.js",
  "build": "next build",
  "start": "NODE_ENV=production node server.js"
},

app.json

{
  "scripts": {
    "dokku": {
      "predeploy": "npm run build"
    }
  }
}

Procfile

web: npm start -- --port $PORT
Leave a Reply

Your email address will not be published. Required fields are marked *