In standalone mode, `server.js` hardcoded NODE_ENV as production
Nobody has claimed this yet.
- 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
- Run
npm run build - See
server.jsoutput file - Run
node .next/standalone/server.js - See the logs that says it's production environment
From empty nextjs repo
- Create Next App
- Configure
next.config.jsoutputto bestandalone - Set
NODE_ENVasteston your computer:export NODE_ENV=test - Create a
.env.testfile - 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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