Cristian Silva
Cristian Silva

Reputation: 3

Deploy 2 different prod servers with 2 different env vars in NextJs

In my Next.js project, I'm trying to deploy a new version of the app to a development server that uses environment variables from .env.development. Here's the process that we follow:

  1. Develop the feature
  2. Test locally using next dev
  3. Deploy to a dev server (replica of prod using dev env vars)
  4. Deploy to a prod server using next build && next export && next start

My problem is with step 3, next build and next start use the .env.production environment variables, which is not what we want for the dev server.

I'm aware that Next.js only has three environments - production, development, and test - and that .env.development is used by next dev and .env.production is used by next start.

How can I deploy the project to the dev server using the .env.development environment variables? I've tried setting the NODE_ENV environment variable to development when running next build and next start, but it still uses the .env.production environment variables.

Here is what I've tried

package.json

"scripts": {
  "build:dev": "NODE_ENV=development next build && next export",
  "start:dev": "NODE_ENV=development next start"
}```

I can't change the process, so I need to figure out a solution, if there is no out of the box solution, I'll change the deployment pipeline to copy the .env.development over the .env.production, but I'd like to avoid it.

Upvotes: 0

Views: 319

Answers (1)

kca
kca

Reputation: 6123

Next.js can do a production build and a development build, which are very different "modes", e.g. the development build includes a lot of debug information and hot-reloading.

What you actually need is a second Next.js production build (i.e. using .env.production), even if you are not deploying it to your "production server" but to a different server, which you happen to call "development server" (and with a different environment).

As you could theoretically want to have an arbitrary amount of different environments and/or servers, this is outside of what Next.js would handle.

You need to manage 2 different .env.production files yourself, using e.g. deployment scripts or server configurations.

Upvotes: 0

Related Questions