[오류해결] Error [ERR_REQUIRE_ESM]: require() of ES Module ~ node_modules/cliui/build/index.cjs not supported.
오류 발생
yarn test
명령어로 테스트 코드를 실행하려는데 아래와 같이 오류가 발생했다.
- yarn v1.22.21
- node 20.10.0
오류 상세
yarn test
yarn run v1.22.21
$ jest
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/iksflow/${PROJECT_ROOT}/node_modules/string-width/index.js from /Users/iksflow/${PROJECT_ROOT}/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js in /Users/iksflow/${PROJECT_ROOT}/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/Users/iksflow/${PROJECT_ROOT}/node_modules/cliui/build/index.cjs:291:21)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
원인 분석
개발환경의 의존성 관리 도구인 Yarn V1으로 설치된 의존성이 CommonJS의 import 문법인 require를 사용하고 있는데 이를 ESM형태로 인식하지 못해 발생하는 것으로 보인다.
해결 방법
찾아보니 Jest의 문제가 아니라 Yarn Classic(V1)에서 발생하는 오류라고한다.
Yarn Berry(V2+) 버전으로 업그레이드하면 오류를 해결할 수 있다.
Yarn Berry로 마이그레이션 하는 방법 포스트를 참고해서 Yarn 버전을 업그레이드하면 오류가 더 이상 발생하지 않는다.
!참고!
yarn.lock 파일을 삭제하고 yarn install을 실행하는 방법으로도 해결이 가능하다.
하지만 이 방법은 의존성에 변경이 생길 때마다 Error [ERR_REQUIRE_ESM] 오류가 다시 발생한다.
따라서 재발을 방지하는 방법인 Yarn 버전 마이그레이션을 추천한다.
참고 자료
'Frontend > Error' 카테고리의 다른 글
댓글
이 글 공유하기
다른 글
-
[오류해결] VSCode - Cannot find name 'test'. Do you need to install type definitions for a test runner?
[오류해결] VSCode - Cannot find name 'test'. Do you need to install type definitions for a test runner?
2024.03.11 -
[오류해결] ESLint 'test' is not defined. no-undef
[오류해결] ESLint 'test' is not defined. no-undef
2024.03.11 -
[오류해결] 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.
[오류해결] 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.
2024.03.10 -
[오류해결] Jest encountered an unexpected token
[오류해결] Jest encountered an unexpected token
2024.03.04