Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling createServer sets process.env.NODE_ENV #18712

Open
7 tasks done
theoephraim opened this issue Nov 19, 2024 · 3 comments
Open
7 tasks done

calling createServer sets process.env.NODE_ENV #18712

theoephraim opened this issue Nov 19, 2024 · 3 comments

Comments

@theoephraim
Copy link

Describe the bug

I was tracking down an issue where process.env.NODE_ENV was being set automatically somehow, and I have tracked it down to calling vite's createServer. This was definitely unexpected! Using a default value make sense, but I would not expect it to set it on process.env

some context - I'm building a config management tool (https://dmno.dev), so anything passed in via env vars are treated as overrides and take precedence over the other config values/resolution rules.

Reproduction

https://stackblitz.com/edit/vitejs-vite-mepepx

Steps to reproduce

npm install
node main.js

System Info

System:
    OS: macOS 13.6.7
    CPU: (12) arm64 Apple M2 Max
    Memory: 1.84 GB / 96.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/Library/Caches/fnm_multishells/67617_1732054910036/bin/node
    Yarn: 1.22.22 - ~/Library/Caches/fnm_multishells/67617_1732054910036/bin/yarn
    npm: 10.2.4 - ~/Library/Caches/fnm_multishells/67617_1732054910036/bin/npm
    pnpm: 9.6.0 - ~/Library/Caches/fnm_multishells/67617_1732054910036/bin/pnpm
    bun: 1.1.21 - /opt/homebrew/bin/bun
  Browsers:
    Chrome: 131.0.6778.71
    Safari: 16.6
  npmPackages:
    vite: ^5.4.8 => 5.4.8

Used Package Manager

pnpm

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Nov 20, 2024

This technically happens in resolveConfig and I was also surprised by this behavior on Remix remix-run/remix#8365.

if (!isNodeEnvSet) {
process.env.NODE_ENV = defaultNodeEnv
}

Looking through the commits, this has been discussed in #9274 and the idea is to move automatic NODE_ENV assignment from resolveConfig (or any programatic API surface) to vite CLI level. (but this hasn't been implemented yet or fully agreed yet?)

@theoephraim
Copy link
Author

Thanks for the info! Will dig through the discussion. Moving that to the cli definitely makes sense to me.

@bluwy
Copy link
Member

bluwy commented Nov 20, 2024

There isn't a concrete plan, but I'd personally still have the logic in createServer or build instead of just the CLI. Some may also be surprised that the CLI or programmatic API would be inconsistent otherwise, so there isn't a silver bullet here. Perhaps we can document this better if it's unclear from the docs, but I think the behaviour at the moment is intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants