Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-fetch

Package Overview
Dependencies
Maintainers
5
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-fetch

A light-weight module that brings window.fetch to node.js

  • 2.7.0
  • release-2.x
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
55M
decreased by-5.26%
Maintainers
5
Weekly downloads
 
Created

What is node-fetch?

The node-fetch package is a light-weight module that brings window.fetch to Node.js. It is designed to provide a fetch-first, URL-friendly way to access resources across the network.

What are node-fetch's main functionalities?

Simple GET Request

This code performs a simple GET request to the GitHub API and logs the response data.

const fetch = require('node-fetch');

fetch('https://api.github.com/users/github')
  .then(response => response.json())
  .then(data => console.log(data));

POST Request with JSON

This code sends a POST request with a JSON body to httpbin.org and logs the response JSON.

const fetch = require('node-fetch');

fetch('https://httpbin.org/post', {
  method: 'post',
  body:    JSON.stringify({foo: 'bar'}),
  headers: { 'Content-Type': 'application/json' },
})
.then(res => res.json())
.then(json => console.log(json));

Handling Network Errors

This code attempts to fetch a resource from an invalid domain and catches network errors.

const fetch = require('node-fetch');

fetch('https://domain.invalid')
  .catch(err => console.error('Network error:', err));

Stream Response

This code fetches an image from GitHub and streams it to a file.

const fetch = require('node-fetch');
const fs = require('fs');

fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
  .then(res => {
    const dest = fs.createWriteStream('./octocat.png');
    res.body.pipe(dest);
  });

Other packages similar to node-fetch

Keywords

FAQs

Package last updated on 23 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc