Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
FetchModule
✓ import { FetchModule } from 'nestjs-fetch'
✗ import { FetchModule } from 'nestjs-fetch/dist'
You must import from the package root, not from a subpath, as the package ships with proper exports.
FetchService
✓ import { FetchService } from 'nestjs-fetch'
✗ import FetchService from 'nestjs-fetch'
FetchService is a named export, not the default export. Also works with destructuring in constructor.
FetchModuleOptions
✓ import { FetchModuleOptions } from 'nestjs-fetch'
✗ import { FetchModuleOptions } from 'nestjs-fetch/dist'
This is a TypeScript type/interface; available as a named export. In JavaScript, you can skip this import.
Shows how to import FetchModule with a base URL, inject FetchService, and make a GET request returning parsed JSON.
import { Module } from '@nestjs/common';
import { FetchModule, FetchService } from 'nestjs-fetch';
@Module({
imports: [FetchModule.register({ baseUrl: 'https://api.example.com' })],
providers: [AppService],
})
export class AppModule {}
@Injectable()
export class AppService {
constructor(private readonly fetch: FetchService) {}
async getData(): Promise<any> {
const response = await this.fetch.get('/data');
return response.json();
}
}
Debug
Known issues
breakingRequires Node >=17.5 with fetch() flagged on, or Node >=18.0. Older Node versions will throw ReferenceError: fetch is not defined.fixUpgrade to Node >=18.0 or use Node 17.5+ with --experimental-fetch flag.
affects: <0.1.0 || all versions
deprecatedThe package is at version 0.2.0; breaking changes may occur before reaching 1.0.0.fixLock dependency version and review changelog before upgrading.
affects: >=0.1.0
gotchaFetchService returns native Response objects, not AxiosResponse. Methods like .json() and .text() are async and must be awaited.fixUse await response.json() instead of response.data. Do not assume response.data exists.
affects: >=0.1.0
gotchaThe module does not automatically convert non-2xx HTTP status codes to thrown exceptions. You must check response.ok yourself.fixAdd if (!response.ok) throw new Error(...) after the fetch call.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: fetch is not defined
Node version <18 and no polyfill enabled.
fixUpgrade to Node >=18.0 or run with --experimental-fetch flag on Node 17.5+.
Cannot read properties of undefined (reading 'get')
The FetchService was not properly injected or the module was not imported.
fixEnsure FetchModule is imported in the module where the service is used, and that the provider is properly injected via constructor.
The "uri" argument must be of type string. Received type object
Passed a non-string, non-URL object as the first argument to a FetchService method.
fixPass a valid string URL or a URL object. Example: this.fetch.get('https://example.com') Audit
Dependencies
@nestjs/commonrequiredPeer dependency required for module and service decorators.