vercel / vercel/next.js

In standalone mode, `server.js` hardcoded NODE_ENV as production

Open
#58,294 7 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Output
Dominant language
JavaScript
Stars
142k
Forks
32.5k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

Link to the code that reproduces this issue

https://github.com/HugoDerigny/standalone-production-env-bug

To Reproduce

From above repo

  1. Run npm run build
  2. See server.js output file
  3. Run node .next/standalone/server.js
  4. See the logs that says it's production environment

From empty nextjs repo

  1. Create Next App
  2. Configure next.config.js output to be standalone
  3. Set NODE_ENV as test on your computer: export NODE_ENV=test
  4. Create a .env.test file
  5. Run next build
    -> You will see that the build take envs from .env.test.
$ next build
   ▲ Next.js 14.0.2
   - Environments: .env.test

BUT

In build files : .next > standalone > server.js you can see that NODE_ENV is hardcoded :

/** l.5 */ process.env.NODE_ENV = 'production'
Current vs. Expected behavior

Current behavior:
NODE_ENV is hardcoded as production

Expected behavior
server.js must not redefine NODE_ENV if it's already defined as test since it's the only other possible value.

Verify canary release
  • I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: 1.22.19
  pnpm: 7.29.1
Relevant Packages:
  next: 14.0.3-canary.0
  eslint-config-next: 14.0.2
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: standalon
Which area(s) are affected? (Select all that apply)

Standalone mode (output: "standalone")

Additional context

Even when using next build if you console.log(process.env.NODE_ENV) in the root layout.tsx, it logs production in the build trace.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with standalone output generation and the generated .next/standalone/server.js, using the linked reproduction or the listed next build steps with output: "standalone". Trace how NODE_ENV is assigned and inspect nearby standalone tests, then verify that an existing NODE_ENV is preserved and the generated server behaves as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, node.js
Domain
backend, build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.