Registry / devops / nestjs-fetch

nestjs-fetch

JSON →
library0.2.0jsnpmunverified

A lightweight NestJS wrapper around the native Fetch API, designed as an alternative to @nestjs/axios for making HTTP requests in NestJS applications. Current stable version is 0.2.0. It leverages Node's native fetch() (available since Node 18) and integrates seamlessly with NestJS modules via FetchModule and FetchService. Compared to @nestjs/axios, it has a simpler API that returns native Response objects and supports both synchronous and asynchronous configuration, including base URL resolution for relative URLs. Requires Node >=17.5 and NestJS >=8. Note: not a drop-in replacement; API differs significantly from HttpModule.

npm install nestjs-fetch
INSTALL
IMPORT
SIG · NESTJS-FETCH
N
nestjs-fetch
devopsjavascriptv0.2.0
harness data pending
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.
fix
Upgrade 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.
fix
Lock 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.
fix
Use 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.
fix
Add 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.
fix
Upgrade 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.
fix
Ensure 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.
fix
Pass a valid string URL or a URL object. Example: this.fetch.get('https://example.com')
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
@nestjs/commonrequiredPeer dependency required for module and service decorators.
Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
nestjs-fetch — npm install nestjs-fetch · libregistry