
"Cannot use import statement outside a module" 오류 해결
•
2024-10-08 16:38
NodeJS를 학습하던 도중에 패키지를 가져오기위해 require가 아닌 import를 사용했더니 발생한 문제를 해결해보자
package.json에 타입 추가
while과 if를 통한 방법입니다.
기본적으로 package.json 에 type이 없다면 모듈 처리방식이 common.js 의 require 방식이된다. 따라서, ES6의 import - export를 사용하려면 루트 디렉토리에 있는 package.json 파일에 "type": "module"를 추가해야한다.
{
'name': 'study-nodejs',
'version': '1.0.0',
'type': 'module',
'description': '',
'main': 'index.js',
'scripts': {
'test': 'echo \'Error: no test specified\' && exit 1'
},
'author': '',
'license': 'ISC',
'dependencies': {
'figlet': '^1.7.0'
}
}