Skip to content

fix: support PORT=0 for random port assignment - #4000

Merged
pi0 merged 2 commits into
nitrojs:mainfrom
wadefletch:fix/port-zero-support
Feb 5, 2026
Merged

pi0 merged 2 commits into
nitrojs:mainfrom
wadefletch:fix/port-zero-support

Conversation

@wadefletch

@wadefletch wadefletch commented Feb 4, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Linked issue

N/A - Minor fix discovered while using the library.

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

The current port parsing pattern in all runtime files:

const port = Number.parseInt(process.env.NITRO_PORT || process.env.PORT || "") || 3000;

Has two issues with PORT=0:

  1. Number.parseInt("0") returns 0
  2. 0 || 3000 evaluates to 3000 because 0 is falsy in JavaScript

This prevents users from using PORT=0 for random port assignment (a common pattern where the OS assigns an available port).

Solution: Replace the double-|| pattern with explicit NaN checking:

const _parsedPort = Number.parseInt(process.env.NITRO_PORT ?? process.env.PORT ?? "");
const port = Number.isNaN(_parsedPort) ? 3000 : _parsedPort;

This correctly handles:

  • PORT=0 → port is 0 (random assignment)
  • PORT not set → port is 3000 (default)
  • PORT="8080" → port is 8080
  • PORT="abc" → port is 3000 (invalid input fallback)

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@wadefletch
wadefletch requested a review from pi0 as a code owner February 4, 2026 19:10
@vercel

vercel Bot commented Feb 4, 2026

Copy link
Copy Markdown

@wadefletch is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Feb 4, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Port parsing refactor across runtime presets: environment values are read into an intermediate _parsedPort using nullish coalescing, then port is set to 3000 if _parsedPort is NaN, otherwise port uses _parsedPort.

Changes

Cohort / File(s) Summary
Port parsing refactor
src/presets/bun/runtime/bun.ts, src/presets/deno/runtime/deno-server.ts, src/presets/node/runtime/node-cluster.ts, src/presets/node/runtime/node-server.ts
Replaced direct parse-and-fallback with two-step NaN-aware parsing: read NITRO_PORT or PORT into _parsedPort via nullish coalescing, then set port to 3000 when _parsedPort is NaN, otherwise use _parsedPort. Preserves behavior for numeric values like 0.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title follows conventional commits format with 'fix:' prefix and clearly summarizes the main change of supporting PORT=0 for random port assignment.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the problem with port parsing, the root cause, and the solution implemented across all runtime files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Replace the double-|| pattern with explicit NaN checking so that
PORT=0 and NITRO_PORT=0 correctly request random port assignment
from the OS instead of being treated as falsy and defaulting to 3000.

Co-authored-by: Cursor <[email protected]>
@wadefletch
wadefletch force-pushed the fix/port-zero-support branch from cc4a16b to 73b8e81 Compare February 4, 2026 19:12
Comment thread src/presets/bun/runtime/bun.ts Resolved
@pkg-pr-new

pkg-pr-new Bot commented Feb 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitrojs/nitro@4000

commit: 288f12c

@pi0
pi0 merged commit acd7198 into nitrojs:main Feb 5, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants