tinyhumansai/openhuman · error · Error
${label} must be an integer between 1 and 65535
Error message
${label} must be an integer between 1 and 65535 What it means
scripts/mock-api-server.mjs validates the --port/-p argument with parsePortValue(): the value must Number()-convert to an integer strictly greater than 0 and at most 65535. Anything else — non-numeric, 0, negative, 70000, NaN — throws and main().catch exits 1. Note the sibling case: a --port value that is missing or itself starts with '-' exits 2 with a different message before this check runs.
Source
Thrown at scripts/mock-api-server.mjs:15
#!/usr/bin/env node
import process from "node:process";
import { DEFAULT_PORT, startMockServer, stopMockServer } from "./mock-api-core.mjs";
function usage() {
return "Usage: node scripts/mock-api-server.mjs [--port <port>]";
}
function parsePortValue(value, label) {
const port = Number(value);
if (Number.isInteger(port) && port > 0 && port <= 65535) {
return port;
}
throw new Error(`${label} must be an integer between 1 and 65535`);
}
function readPortArg() {
const idx = process.argv.findIndex((arg) => arg === "--port" || arg === "-p");
if (idx >= 0) {
const value = process.argv[idx + 1];
if (!value || value.startsWith("-")) {
console.error("[mock-api-server] --port requires an integer between 1 and 65535");
process.exit(2);
}
try {
return parsePortValue(value, "--port");
} catch (err) {
console.error(`[mock-api-server] ${err.message}`);
process.exit(2);
}
}
if (process.argv.includes("--help") || process.argv.includes("-h")) {View on GitHub (pinned to a221052e0d)
Solutions
- Pass an integer in 1..65535, e.g. `--port 4173`
- If the port comes from a variable, default it first: `--port "${MOCK_PORT:-4173}"`
- Or omit --port entirely — the server falls back to MOCK_API_PORT / E2E_MOCK_PORT env vars and then DEFAULT_PORT, which is always valid
Example fix
# before
node scripts/mock-api-server.mjs --port $PORT # PORT unset → '' → invalid
# after
node scripts/mock-api-server.mjs --port "${PORT:-4173}" Defensive patterns
Strategy: validation
Validate before calling
function resolvePort(raw) {
const n = Number(raw);
if (Number.isInteger(n) && n > 0 && n <= 65535) return n;
console.error(`Invalid port '${raw}' — falling back to default`);
return undefined; // or exit non-zero in strict callers
} Type guard
/** Narrows to a TCP-port-safe integer in 1..65535. */
function isValidPort(value) {
const n = Number(value);
return Number.isInteger(n) && n > 0 && n <= 65535;
} Try / catch
try {
startMockServer(readPortArg());
} catch (err) {
if (err.message.includes('must be an integer between 1 and 65535')) {
console.error('Pass --port <1-65535> or set MOCK_API_PORT; see --help');
process.exit(2);
}
throw err;
} Prevention
- Default expanded variables: `--port "${PORT:-4173}"` — an unset var expands to '' which Number()s to 0 and fails
- Reuse the mock API's own env fallback (MOCK_API_PORT / E2E_MOCK_PORT) instead of hand-passing ports everywhere
When it happens
Trigger: `node scripts/mock-api-server.mjs --port 0`, `--port abc`, `--port 70000`, or an unset shell var expanding to empty (`--port $PORT` with PORT empty → Number('') === 0 → invalid).
Common situations: CI passing an unconfigured port variable; picking a port outside the ephemeral range; typo'd numeric value.
Related errors
- unknown argument: ${arg}
- Failed to parse service CLI output as JSON: parsed value doe
- paths must be repo-relative, not absolute (got "${p}")
- must be an array
- ${label} must be a positive integer
AI-assisted analysis of tinyhumansai/openhuman@a221052e0d (2026-08-16).
Data as JSON: /api/errors/73a568925ee2b060.
Report an issue: GitHub.