본문 바로가기
개발/에러해결

SyntaxError: Cannot use import statement outside a module

by amkorousagi 2022. 10. 22.

에러 내용


text
SyntaxError: Cannot use import statement outside a module

SyntaxError: Cannot use import statement outside a module

 

에러 원인


import는 type이 module인 파일에서만 쓸 수 있다.

 

해결 방법


1. package.json에서 type을 module이라고 명시해준다.

 

수정 전 코드

text
{ "name": "front", "version": "0.1.0", "private": true . . . }

 

수정 후 코드

text
{ "name": "front", "version": "0.1.0", "private": true, "type": "module" . . . }

 

2. 또는 import 대신에 require을 사용한다.

 

수정 전 코드

text
import React from "react";

 

수정 후 코드

text
const React = require("react");

 

참고 자료


 

SyntaxError: Cannot use import statement outside a module

I've got an ApolloServer project that's giving me trouble, so I thought I might update it and ran into issues when using the latest Babel. My "index.js" is: require('dotenv').config() imp...

stackoverflow.com

 

댓글