Skip to content

Pass opts.dispatcher to the top-level undici functions or call the dispatcher instance method directly. #5014

@ShenHongFei

Description

@ShenHongFei

Bug Description

#5007

This PR causes the following usage to throw an error: 'Pass opts.dispatcher to the top-level undici functions or call the dispatcher instance method directly.'

I believe undici.request should fall under 'top-level undici functions,' right?

test.ts

import undici from 'undici'

await undici.request('https://qq.com', {
    dispatcher: new undici.Agent({ allowH2: true })
})

Before #5007 the above code run successfully

Logs & Screenshots

T:/t/nodejs/out/Release/node.exe D:/0/test/test.ts
node:internal/deps/undici/undici:8752
        throw new InvalidArgumentError('opts.dispatcher is not supported by instance methods. Pass opts.dispatcher to the top-level undici functions or call the dispatcher instance method directly.')
              ^

InvalidArgumentError: opts.dispatcher is not supported by instance methods. Pass opts.dispatcher to the top-level undici functions or call the dispatcher instance method directly.
    Agent.dispatch() 于 internal/deps/undici/undici:8752:15
    Agent.request() 于 internal/deps/undici/undici:699:10
    node:internal/deps/undici/undici:690:15
    new Promise() 于 <anonymous>
    Agent.request() 于 internal/deps/undici/undici:689:12
    Module.<anonymous>() 于 internal/deps/undici/undici:26047:19
    file:///D:/0/test/test.ts:3:14
    ModuleJob.run() 于 internal/modules/esm/module_job:437:25
    async node:internal/modules/esm/loader:642:26
    asyncRunEntryPointWithESMLoader() 于 internal/modules/run_main:101:5 {
  code: 'UND_ERR_INVALID_ARG'
}

Environment

node.js: latest main branch
undici: main (4e4c2a3 2026.04.09)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions