Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

await on the request module gives undefined or never waits

Errorawait request(url) returns immediately and the response is undefined

Short answer

await only waits for a promise. The request package works with callbacks and streams, so await request(...) does not wait for the response. The package has been fully deprecated since February 2020. Use the built-in fetch, or wrap the callback API with util.promisify if you must keep request.

  • Applies to Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check what request returns and whether the package prints a deprecation notice on install.

    npm ls request
  2. 2Match what you see, then jump to its fix

    1. 1The code after await runs before the response arrives, or the result is not a responserequest is a callback API, so await has no promise to wait forFix
    2. 2Many call sites or a dependency you cannot rewrite todayYou need to keep request for nowFix

What does the error look like?

  • await request(url) returns immediately and the response is undefined
  • request has been deprecated

Cause 1 of 2

request is a callback API, so await has no promise to wait for

1Check

The README says request supports streaming and callback interfaces natively, and points to wrapper packages for promises.

grep -rn "require('request')" src/

2Fix

Replace it with the global fetch, which Node.js has had built in since version 18 and which no longer needs a flag.

const res = await fetch('https://api.example.com/items');
if (!res.ok) throw new Error('HTTP ' + res.status);
const data = await res.json();

3Verify

Run the file and print the parsed data.

node app.mjs

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 2

You need to keep request for now

1Check

The README says util.promisify (Node.js 8 and later) can turn a callback function into one that returns a promise. It also says request is fully deprecated as of 11 February 2020 and no changes are expected.

npm ls request

2Fix

Promisify the call as a stop-gap and plan the move to fetch.

const { promisify } = require('node:util');
const request = require('request');
const get = promisify(request.get);
const res = await get('https://api.example.com/items');
console.log(res.statusCode);

3Verify

Run the script and check the status code prints.

node app.js

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Do not add request to new projects; it receives no changes.
  • Use fetch in new code and replace old call sites during normal work.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.