9

I would like to set up two different builds for a next.js application.

package.json:

...
"scripts": {
  "dev": "next dev",
  "debug": "cross-env NODE_OPTIONS='--inspect' next dev",
  "build:dev": "set NODE_ENV=development && next build",
  "build:prod": "set NODE_ENV=production && next build",
  "start": "node server.js"
},
"dependencies": {
  "bootstrap": "^5.0.0-beta3",
  "lodash": "^4.17.21",
  "next": "^10.0.0",
  "react": "17.0.1",
  "react-dom": "17.0.1",
  "react-scrollspy": "^3.4.3",
  "sass": "^1.32.11"
},
"devDependencies": {
   "cross-env": "^7.0.3"
}
...

Then I created .env.development and .env.production with different hostname and port, like this:

HOST=127.0.0.3
PORT=9999
API_DOMAIN=http://$HOST:$PORT/API

But if I run npm run build:dev it displays this:

$ npm run build:dev

> [email protected] build:dev
> set NODE_ENV=development && next build

warn  - You are using a non-standard "NODE_ENV" value in your environment. This creates inconsistencies in the project and is strongly advised against. Read more: https://nextjs.org/docs/messages/non-standard-node-env
info  - Loaded env from W:\web\projects\xfb\xfbweb\react\.env.production
info  - Using webpack 4. Reason: future.webpack5 option not enabled https://nextjs.org/docs/messages/webpack5
info  - Checking validity of types...
info  - Creating an optimized production build...
info  - Compiled successfully
info  - Collecting page data...
FetchError: request to http://127.0.0.3:9999/API/GET/blog/?mode_cd=summary&offset=1&limit=2&language_cd=eng failed, reason: connect ECONNREFUSED 127.0.0.3:9999
...

It seems this uses the .env.production. How can I force this to use the .env.development in this case?
By the way, if i run npm run build:prod it also uses the .env.production.
I tried to update next to v12.1.0 but still do the same.

2
  • Running into exactly the same, did you solve it? Commented Jun 25, 2022 at 6:51
  • Any proper solution to this issue? Commented May 24, 2024 at 22:56

1 Answer 1

4

We use Docker containers so for the development we had to copy the file .env.development to .env.production before npm run build command in dockerfile. That is the only way we could solve this problem.

Sign up to request clarification or add additional context in comments.

1 Comment

This solution is fine, I just hate that it's currently the best option. How has this been an issue for so long?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.