node.js - require 사용 시 Typescript 오류 - 이 표현식은 호출할 수 없습니다. 타입 'typeof import(...)'에는 호출 서명이 없습니다.ts(2349)
저는 이 스크립트를 실행하려고 하고 있습니다
const fetch = require('node-fetch');
function test() {
fetch('https://google.com')
.then(res => res.text())
.then(text => console.log(text))
}
test();
하지만 나는 이 오류가 발생해
이 표현식은 호출할 수 없습니다. 타입 'typeof import("(...)/node_modules/node-fetch/@types/index")'에는 호출 시그니처가 없습니다.ts(2349)
내가 import를 사용할 때는 작동하지만
import fetch from 'node-fetch';
왜 그리고 어떻게 고치나요?
답변 #1
지출자의 댓글에 따라, 이 구조 분해를 사용하도록 require를 변경할 수 있습니다:
const {default : fetch} = require('node-fetch');
이것은 비슷한 상황에서 저에게 효과가 있었습니다(유사한 API를 가진 노드에서 axios를 사용할 때).
