Skip to content

process.env is undefined in local production build even with dotenv-cli installed and accessing env server-only #13830

Closed
@nikolailehbrink

Description

@nikolailehbrink

I'm using React Router as a...

framework

Reproduction

https://stackblitz.com/edit/github-qphddjff?file=app%2Froutes%2Fhome.tsx

  1. Open the repro
  2. Clear the terminal and build it locally with npm run build
  3. Start the preview server with npm run start
  4. See that it logs undefinedin the loader, although I even installed dotenv-cli which according to this discussion should fix it
  5. When uncommenting the define in vite-config.ts it works, however I get a warning while building, that this could be a security flaw

System Info

System:
    OS: macOS 15.5
    CPU: (10) arm64 Apple M1 Max
    Memory: 222.20 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.13.0 - ~/.nvm/versions/node/v22.13.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.13.0/bin/yarn
    npm: 10.9.2 - ~/.nvm/versions/node/v22.13.0/bin/npm
    pnpm: 10.12.1 - ~/.nvm/versions/node/v22.13.0/bin/pnpm
    bun: 1.1.45 - ~/.nvm/versions/node/v22.13.0/bin/bun
  Browsers:
    Chrome: 137.0.7151.105
    Safari: 18.5
  npmPackages:
    @react-router/dev: ^7.5.3 => 7.6.2 
    @react-router/node: ^7.5.3 => 7.6.2 
    @react-router/serve: ^7.5.3 => 7.6.2 
    react-router: ^7.5.3 => 7.6.2 
    vite: ^6.3.3 => 6.3.5

Used Package Manager

npm

Expected Behavior

When starting the production build locally, all environment variables from .env should be available in server-side loader functions.

Actual Behavior

I’m running into the same issue as described in this discussion, but even with dotenv-cli installed and the recommended setup, environment variables are not available during a local production build.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions