Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
HttpModule
✓ import { HttpModule } from 'nestjs-http-promise'
✗ const { HttpModule } = require('nestjs-http-promise')
Use named import for the module. CommonJS `require` is generally not used in modern NestJS TypeScript projects and may lead to module resolution issues.
HttpService
✓ import { HttpService } from 'nestjs-http-promise'
✗ import HttpService from 'nestjs-http-promise'
HttpService is a named export. Attempting a default import will result in 'Module ''nestjs-http-promise'' has no default export' or undefined.
HttpModuleOptionsFactory
✓ import { HttpModuleOptionsFactory } from 'nestjs-http-promise'
✗ import { IHttpModuleOptionsFactory } from 'nestjs-http-promise'
This interface is crucial for implementing asynchronous module configuration via `registerAsync` and must be imported correctly.
This quickstart demonstrates how to install, configure (asynchronously), inject, and use `nestjs-http-promise` to perform promise-based HTTP requests with built-in retries, fetching and creating a test post.
import { Module, Injectable, INestApplication } from '@nestjs/common';
import { HttpModule, HttpService, HttpModuleOptionsFactory, HttpModuleOptions } from 'nestjs-http-promise';
import { NestFactory } from '@nestjs/core';
// Example of an async configuration service
@Injectable()
class HttpConfigService implements HttpModuleOptionsFactory {
async createHttpOptions(): Promise<HttpModuleOptions> {
// Simulate fetching configuration data asynchronously, e.g., from a config service or environment variables
const configurationData = await Promise.resolve({
baseURL: 'https://jsonplaceholder.typicode.com',
timeout: 5000,
maxRetries: 3,
isBetterStackTraceEnabled: true // Enable improved stack traces by default
});
return {
baseURL: configurationData.baseURL,
timeout: configurationData.timeout,
retries: configurationData.maxRetries,
isBetterStackTraceEnabled: configurationData.isBetterStackTraceEnabled,
};
}
}
@Injectable()
class MyApiService {
constructor(private readonly httpService: HttpService) {}
/**
* Fetches a post by ID. This call benefits from the module's configured retries.
*/
async fetchPost(id: number): Promise<any> {
console.log(`Attempting to fetch post ${id}...`);
// The HttpService methods return Promises directly
const response = await this.httpService.get(`/posts/${id}`);
return response.data;
}
/**
* Creates a new post. Retries can be overridden per request.
*/
async createPost(title: string, body: string): Promise<any> {
console.log('Attempting to create a new post...');
const response = await this.httpService.post('/posts', { title, body, userId: 1 }, {
retries: 0 // No retries for create operations for this specific request
});
return response.data;
}
}
@Module({
imports: [
// Asynchronously configure HttpModule using a factory class
HttpModule.registerAsync({
useClass: HttpConfigService,
}),
],
providers: [MyApiService, HttpConfigService], // HttpConfigService must be provided if used with useClass
exports: [MyApiService], // Export the service if it's used by other modules
})
class ApiIntegrationModule {}
async function bootstrap() {
const app: INestApplication = await NestFactory.create(ApiIntegrationModule);
await app.listen(3000, () => {
console.log('NestJS Application listening on port 3000');
});
const apiService = app.get(MyApiService);
try {
const post = await apiService.fetchPost(1);
console.log('\nFetched Post 1:', post);
const newPost = await apiService.createPost('Hello Registry', 'This is a test post from the registry quickstart.');
console.log('\nCreated New Post:', newPost);
} catch (error: any) {
console.error('\nAn error occurred during API calls:');
console.error('Error message:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
console.error('Response data:', error.response.data);
} else if (error.code === 'ECONNABORTED') {
console.error('Request timed out or cancelled.');
}
} finally {
await app.close();
console.log('\nApplication closed.');
}
}
// To run this quickstart, save it as a .ts file in a NestJS project and execute with `ts-node` or compile.
// In a real application, `bootstrap()` would be called from `main.ts`.
bootstrap();
Errors
Common errors & fixes
Nest can't resolve dependencies of the HttpService (?). Please make sure that the argument at index [0] is available in the ApiIntegrationModule context.
HttpModule was not properly imported or configured in the module where HttpService is being injected.
fixVerify that `HttpModule` (either `HttpModule.register()` or `HttpModule.registerAsync()`) is correctly listed in the `imports` array of the module that declares the component injecting `HttpService`.
Error: Cannot find module 'nestjs-http-promise'
The package was not installed or an incorrect import path was used.
fixRun `npm install nestjs-http-promise` or `yarn add nestjs-http-promise`. Check that the import statement matches `import { ... } from 'nestjs-http-promise'`. TypeError: Cannot read properties of undefined (reading 'get') (or similar for post, put, etc.) on HttpService instance.
HttpService was injected but its dependencies (Axios) might not have been properly initialized, or the module was not set up correctly leading to an undefined HttpService.
fixEnsure `HttpModule` is correctly imported in the module that provides `HttpService`. For `registerAsync`, double-check the `useFactory` or `useClass` implementation to ensure valid `HttpModuleOptions` are returned.
Audit
Dependencies
@nestjs/commonrequiredCore NestJS framework dependency, required for module and service integration.
reflect-metadatarequiredRequired by NestJS for dependency injection and decorator functionality.
axiosrequiredUnderlying HTTP client library used for making requests.