vitest-dev/vitest · error · Error
Expected IP address, received
Error message
Expected IP address, received ${address} What it means
The helper IPnumber() parses an IPv4 dotted-quad via /^\d+\.\d+\.\d+\.\d+$/. If the string does not match it throws 'Expected IP address, received ...'. It is invoked from the http-host check in createNetworkModule() on url.hostname whenever that hostname is neither 'localhost' nor '::1'.
Solutions
- Switch the http import to https; https imports are not subject to the IP check.
- Use a loopback hostname (localhost or 127.0.0.1) so IPnumber is never reached.
- Use a non-vm pool (threads/forks) if you must import over http from a named host.
- Avoid remote http imports in tests entirely; fetch the file and import it locally.
Example fix
// before
await import('http://my-service.local/fixture.js')
// after
await import('https://my-service.local/fixture.js') Defensive patterns
Strategy: validation
Validate before calling
function isIPv4(s){ return /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.test(s) }
const h = new URL(u).hostname
if (u.startsWith('http://') && h!=='localhost' && h!=='::1' && !isIPv4(h)) {
throw new Error('http import requires an IPv4/localhost host or https')
} Type guard
function isValidIPv4(address){
const m = address.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
return !!m && m.slice(1).every(n => +n >= 0 && +n <= 255)
} Prevention
- Avoid http:// imports with domain hostnames in the vm pool.
- Use https:// so the IP-number check is bypassed.
- Run remote-dependent tests in threads/forks pools.
When it happens
Trigger: An http:// import in the vm pool whose hostname is a domain name (e.g. http://example.com/x.js) or a non-IPv4 literal (e.g. an IPv6 address other than ::1). Because the hostname is not 'localhost' or '::1', IPnumber(url.hostname) is called on a non-IPv4 string and throws before the [361] comparison can run.
Common situations: Importing from a CDN or named host over http in the vm pool. Note this masks error [361]: for domain hostnames you get [362] rather than the friendlier 'use https instead' message.
Related errors
- import of ' ' by undefined is not supported: http can only…
- Cannot import " ": its vm context was torn down.
- Cannot import " ": the test context was torn down.
- Environment " " is not a valid environment. Path " "…
- Environment doesn't provide a valid context. It should be…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/8967609dce535c73.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/runtime/vm/esm-executor.ts:484
}
if (mime === 'application/json') {
const module = this.createJsonModule(identifier, code as string)
this.moduleCache.set(identifier, module)
return module
}
return this.createEsModule(identifier, () => code as string)
}
}
function IPnumber(address: string) {
const ip = address.match(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
if (ip) {
return (+ip[1] << 24) + (+ip[2] << 16) + (+ip[3] << 8) + +ip[4]
}
throw new Error(`Expected IP address, received ${address}`)
}
function IPmask(maskSize: number) {
return -1 << (32 - maskSize)
}
View on GitHub (pinned to 1fa9837ec2)