Request Promise Core
request-promise-core is a foundational library that provides Promise support for the now-deprecated 'request' HTTP client. It is not intended for direct use by most applications; instead, it serves as the core logic for higher-level packages like `request-promise`, `request-promise-any`, `request-promise-bluebird`, and `request-promise-native`. Published last on July 22, 2020, this package's development has ceased, mirroring the abandonment of its peer dependency, 'request'. It offers a mechanism to inject Promise capabilities (e.g., `.then()`, `.catch()`) into the `request` object via a `configure` function, requiring a compatible Promise implementation to be explicitly passed. Due to the deprecation of its upstream 'request' dependency, this package is considered obsolete and should not be used in new projects.
Common errors
-
Error: Cannot find module 'request'
cause The 'request' package is defined as a peer dependency, meaning it must be installed separately by the consumer project, but it was not.fixInstall the 'request' package: `npm install --save request`. -
TypeError: request(...).then is not a function
cause The 'request' instance was not properly patched by `request-promise-core`, or an unpatched 'request' instance was used. This often happens if `require('request')` is called directly before `request-promise-core` has a chance to apply its patches.fixEnsure `request-promise-core`'s `configure` function is called to patch `request`, and that `request` is loaded using `stealthyRequire` as recommended in the documentation to avoid using an unpatched instance. -
TypeError: PromiseImpl must be a thenable constructor
cause The `PromiseImpl` option passed to the `configure` function was either missing or not a valid ES6-compatible Promise constructor.fixEnsure `PromiseImpl: Promise` (for native Promises) or a specific Promise library constructor (e.g., `PromiseImpl: Bluebird`) is provided to the `configure` function.
Warnings
- breaking The underlying 'request' library, which this package extends, was officially deprecated on February 11, 2020. This means 'request-promise-core' and its dependents (like 'request-promise') are also effectively abandoned and should not be used in new projects. Consider migrating to modern alternatives like Axios, `node-fetch`, or `undici`.
- gotcha Directly using `require('request')` before patching its prototype with `request-promise-core` can lead to an unpatched 'request' instance being used by other parts of your application. The README explicitly recommends using `stealthy-require` to prevent such conflicts.
- deprecated The 'request@next' branch for the 'request' library never fully materialized or reached a stable release. Consequently, the configuration helper `require('request-promise-core/configure/request-next')` is obsolete and should not be used.
- gotcha This library is explicitly designed as a core component for other `request-promise` packages and is 'only recommended to use this library directly, if you have very specific requirements.' Most users should have used `request-promise` directly, which itself is now deprecated.
Install
-
npm install request-promise-core -
yarn add request-promise-core -
pnpm add request-promise-core
Imports
- configure
import configure from 'request-promise-core/configure/request2';
const configure = require('request-promise-core/configure/request2'); - stealthyRequire
import stealthyRequire from 'stealthy-require';
const stealthyRequire = require('stealthy-require'); - request
const request = require('request');const request = stealthyRequire(require.cache, function () { return require('request'); });
Quickstart
const stealthyRequire = require('stealthy-require');
const request = stealthyRequire(require.cache, function () {
return require('request');
});
const configure = require('request-promise-core/configure/request2');
configure({
request: request,
PromiseImpl: Promise,
expose: [
'then',
'catch',
'promise'
],
constructorMixin: function (resolve, reject) {
// `this` is the request object
}
});
// 3. Use request with its promise capabilities
request('https://api.example.com/data')
.then(function (body) {
console.log('Received data:', body.substring(0, 100) + '...');
})
.catch(function (err) {
console.error('Request failed:', err.message);
});
// Example with environment variable for a real API call (mocked here)
const API_KEY = process.env.EXAMPLE_API_KEY ?? 'MOCK_API_KEY';
request(`https://api.example.com/secure-data?key=${API_KEY}`)
.then(function (body) {
console.log('Secure data access successful (mocked):', body.substring(0, 100) + '...');
})
.catch(function (err) {
console.error('Secure data access failed (mocked):', err.message);
});