티스토리 뷰

728x90
반응형

fetch

fetch()는 자바스크립트에서 지원하는 URL을 호출하여 불러오는 내장 라이브러리 함수 입니다.

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

 

Using the Fetch API - Web APIs | MDN

The Fetch API provides a JavaScript interface for accessing and manipulating parts of the protocol, such as requests and responses. It also provides a global fetch() method that provides an easy, logical way to fetch resources asynchronously across the net

developer.mozilla.org

 


node-fetch

https://www.npmjs.com/package/node-fetch

 

node-fetch

A light-weight module that brings Fetch API to node.js. Latest version: 3.2.10, last published: 2 months ago. Start using node-fetch in your project by running `npm i node-fetch`. There are 27690 other projects in the npm registry using node-fetch.

www.npmjs.com

node.js 환경에서도 fetch(URL) 함수를 불러서 웹페이지의 데이터를 로드할 수 있습니다.


ERR_REQUIRE_ESM 에러 

작업래포/node_modules/node-fetch/src/index.js  require() of ES modules is not supported. 
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module

 

위와 같은 에러는 node-fetch의 fetch 함수를 이용하려면 es module을 써야합니다.

즉,  import fetch from 'node-fetch'  형태로 fetch 함수를 불러올 수 없음을 의미합니다.

 

에러해결과정

1. node-fetch 삭제하고 v2로 다운그레이드

( @types/node-fetch 추가 설치는 typescript 언어로 작업하는 경우에만 해당합니다.)

$ npm uninstall node-fetch

$ npm install node-fetch@v2

$ npm i @types/node-fetch

 

2. package.json 타입변경

{
	...
	"type": "module",
	...
}

 


[참고자료]

 

728x90
반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함