vercel / next.js

The React Framework
https://nextjs.org
MIT License
124.43k stars 26.55k forks source link

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

Open HugoDerigny opened 9 months ago

HugoDerigny commented 9 months ago

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

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.

leerob commented 8 months ago

https://github.com/vercel/next.js/pull/59005#pullrequestreview-1752814458

msdlisper commented 2 months ago

up

alefcarlos commented 1 month ago

Hey, any news about that ? How could we manage env values for multiple environments ?

msdlisper commented 1 month ago

Hey, any news about that ? How could we manage env values for multiple environments ?

I have an idea to copy the target .env file to .env.local in the deployed script, The .localfile has the highest priority