Skip to content

Instantly share code, notes, and snippets.

@yano3nora
Last active March 13, 2025 07:10
Show Gist options
  • Save yano3nora/39c64dc851fd122b57e93295a7086dce to your computer and use it in GitHub Desktop.
Save yano3nora/39c64dc851fd122b57e93295a7086dce to your computer and use it in GitHub Desktop.
[js: node-fetch] Fetch API for Node. #js

Overview

github.com/node-fetch/node-fetch

v3 から pure esm package になっていて、古い node.js 環境だと使えない可能性が高い。

# v3 (esm only)
$ npm i node-fetch@3
$ npm i -D @types/node-fetch@3

# v2
$ npm i node-fetch@2
$ npm i -D @types/node-fetch@2
import fetch from 'node-fetch'

// get
const getResponse = await fetch('https://api.github.com/users/github')
const getResponseData = await getResponse.json()
console.log(getResponseData)

// post
const postResponse = await fetch('https://httpbin.org/post', {
  method: 'post',
  body: JSON.stringify({ hello: 'world' }),
  headers: {'Content-Type': 'application/json'},
})
const postResponseData = await postResponse.json()
console.log(postResponseData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment