📦import 문을 쓸 수 없음 (모듈 설정)
SyntaxError: Cannot use import statement outside a module
파일이 'import'를 쓰는 최신 방식(ES 모듈)인데, 실행 환경은 옛 방식(require)으로 읽고 있다는 뜻이에요.
🤔 왜 생기나요?
- package.json에 "type": "module" 설정이 없어요
- 옛 방식과 새 방식 파일이 섞였어요
- 파일 확장자(.js/.mjs/.cjs)가 설정과 맞지 않아요
🔧 이렇게 해 보세요
- 프로젝트가 어느 방식을 쓰는지 정하고 하나로 맞춰요
- AI에게 package.json과 에러를 같이 보여 주면 빨라요
🤖 AI에게 이렇게 물어보세요
아래 'Cannot use import statement outside a module' 에러가 나. package.json 설정과 파일 확장자를 확인해서 모듈 방식을 하나로 맞춰 줘. [여기에 에러 전체를 붙여 넣기]
[ ] 부분은 내 상황에 맞게 바꾸고, 에러는 앞뒤 몇 줄까지 넉넉히 복사해 붙여 넣으세요.
관련 용어
같은 분류의 다른 에러
- 모듈을 찾을 수 없음
Module not found: Can't resolve - 명령어를 찾을 수 없음
command not found - 패키지 버전 충돌 (ERESOLVE)
npm ERR! ERESOLVE unable to resolve dependency tree - 포트가 이미 사용 중
EADDRINUSE: address already in use - 타입이 맞지 않음 (TypeScript)
Type 'X' is not assignable to type 'Y' - 권한이 없어 설치 못 함 (EACCES)
EACCES: permission denied - Node 버전이 맞지 않음
The engine "node" is incompatible with this module - 메모리 부족 (heap out of memory)
JavaScript heap out of memory - 코드 검사(ESLint)에 걸려 빌드 실패
Failed to compile — ESLint errors - 파일이나 폴더를 찾을 수 없음 (ENOENT)
ENOENT: no such file or directory