different-ai/openwork · error

PORT must be an integer between 1 and 65535

Error message

PORT must be an integer between 1 and 65535

What it means

The Den gateway validates the PORT environment variable at startup via parsePort: it defaults to 8788, but any provided value that is not an integer in 1..65535 (Number() applied to the raw string) throws 'PORT must be an integer between 1 and 65535', crashing the process during env parsing.

Source

Thrown at ee/apps/den-gateway/src/env.ts:31

  DEN_GATEWAY_LOG_REQUESTS: z.string().optional(),
})

const parsed = EnvSchema.parse(process.env)

function optionalString(value: string | undefined) {
  const trimmed = value?.trim()
  return trimmed ? trimmed : undefined
}

// GH Actions builds pass DEN_GATEWAY_VERSION; Render repo builds rely on RENDER_GIT_COMMIT.
export function resolveGatewayBuildVersion(input: { denGatewayVersion?: string; renderGitCommit?: string }): string | undefined {
  return optionalString(input.denGatewayVersion) ?? optionalString(input.renderGitCommit)
}

function parsePort(value: string | undefined) {
  const port = Number(value ?? "8788")
  if (!Number.isInteger(port) || port <= 0 || port > 65535) {
    throw new Error("PORT must be an integer between 1 and 65535")
  }
  return port
}

function parsePositiveInteger(envName: string, value: string | undefined, fallback: number) {
  const parsedValue = Number(value ?? String(fallback))
  if (!Number.isInteger(parsedValue) || parsedValue <= 0) {
    throw new Error(`${envName} must be a positive integer`)
  }
  return parsedValue
}

function normalizeHttpBaseUrl(envName: string, value: string) {
  let url: URL
  try {
    url = new URL(value)
  } catch {
    throw new Error(`${envName} must be an absolute http or https URL`)

View on GitHub (pinned to 2b7df46e8a)

Solutions

  1. Set PORT to a plain integer string between 1 and 65535 (e.g. PORT=8788)
  2. Check where the value originates — platform-injected values like 'tcp://host:port' must be parsed down to the numeric port
  3. Unset PORT to fall back to the default 8788
  4. Validate your .env / deployment config for quotes, whitespace, or empty expansions

Example fix

// before (.env)
PORT="tcp://10.0.0.1:8080"
// after (.env)
PORT=8080
Defensive patterns

Strategy: validation

Validate before calling

function portIsValid(v) {
  if (v === undefined || v === '') return true // falls back to 8788
  const n = Number(v)
  return Number.isInteger(n) && n > 0 && n <= 65535
}
if (!portIsValid(process.env.PORT)) throw new Error(`Invalid PORT: ${process.env.PORT}`)

Type guard

function isPortValue(v: unknown): v is `${number}` {
  const n = Number(v)
  return typeof v === 'string' && v.trim() !== '' && Number.isInteger(n) && n > 0 && n <= 65535
}

Try / catch

try {
  await import('./env.js') // or start the gateway
} catch (err) {
  if (err instanceof Error && err.message.startsWith('PORT must be an integer')) {
    console.error(`Fix PORT in the environment (got "${process.env.PORT}"); default is 8788`)
    process.exit(1)
  }
  throw err
}

Prevention

When it happens

Trigger: Setting PORT to a non-numeric string ('abc', ''), a quoted value with whitespace/symbols ('"3000"'), a float ('3000.5'), 0, a negative number, or a value above 65535 before starting den-gateway.

Common situations: Copy-pasting a port from a .env template including quotes; platforms injecting PORT like 'tcp://10.0.0.1:8080' (Docker-style URL, which Number() turns into NaN); typos such as '8788 '; compose files passing ${PORT:-} that expands empty.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of different-ai/openwork@2b7df46e8a (2026-09-01). Data as JSON: /api/errors/fb80b4248606ac4f. Report an issue: GitHub.