Skip to content

nitro preview ignores PORT from .env #4970

Closed

Description

@xcvzmoon

Environment

  • Nitro: 3.0.261029-rc
  • Node.js: v26.13.0
  • OS: macOS

Reproduction

  1. Build a minimal Nitro application:

    npx nitro build
  2. Create .env in the project root:

    PORT=14321
  3. With neither PORT nor NITRO_PORT exported in the shell, run:

    npx nitro preview
  4. The server listens on port 3000.

  5. Pass the same variable through the process environment:

    PORT=14321 npx nitro preview
  6. The server now listens on port 14321.

Describe the bug

nitro preview loads environment files, but their PORT value does not apply to the preview listener.

Expected behavior: preview uses PORT from its loaded environment files when no --port flag or existing process environment variable overrides it.

Actual behavior: preview uses port 3000, despite .env defining PORT=14321. Supplying PORT through the shell works.

Additional context

The startup order appears to explain this behavior:

  • src/cli/commands/preview.ts creates the srvx server with manual: true.
  • The srvx Node adapter resolves and stores its listening port during construction.
  • startPreview() subsequently loads environment files in src/preview.ts.
  • server.serve() starts listening using the previously resolved port.

manual: true delays listening, but does not delay port resolution.

For comparison, nitro dev correctly uses PORT from .env.

This report concerns preview's environment-loading order. The built server not automatically loading .env is documented behavior and is unrelated.

Logs

With `PORT=14321` in `.env`:


➜ Listening on: http://localhost:3000/ (all interfaces)


With `PORT=14321` supplied through the process environment:


➜ Listening on: http://localhost:14321/ (all interfaces)

Activity

  1. added
    pending triage
    on Oct 30, 2026
  2. added
    bug
    cli
    and removed
    pending triage
    on Oct 30, 2026
  3. added theissue type on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcli

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions