ERROR

Must use import to load ES Module

처니형 2022. 3. 22. 15:31
- 에러 내용
 
15:18:59.715 [19766] error: uncaughtException: Must use import to load ES Module: /Users/yohank/PROJECT/paycoq-project/messaging-pay/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /Users/yohank/PROJECT/paycoq-project/messaging-pay/node_modules/node-fetch/src/index.js from /Users/yohank/PROJECT/paycoq-project/messaging-pay/routes/auth/token/modules/certification.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/yohank/PROJECT/paycoq-project/messaging-pay/node_modules/node-fetch/package.json.
 
- 발생 원인
 
node-fetch의 버전이 node 버전과 호환되지 않아서 생긴 오류
node 14v에서 fetch 함수를 사용했을 때 발생하는 오류였다.
 
"node-fetch": "^3.2.3",
 
- 해결
 
node-fetch 버전 다운그레이드
npm install node-fetch@2
 
"node-fetch": "^2.6.7"