nodejs에서 나름 MVC패턴으로 구조를 변경하고 있는데 "Cannot use import statement outside a module" 라는 오류가 떴다.
import 구문이 commonjs에서는 사용되지 않고 es6에서 사용하는 것이기 때문이었다.
package.json 파일에 type필드를 "module"로 지정해줘야 했다.
이 type필드에 값이 없거나 commonjs이면 commonjs이므로 "module"로 명시해주고 es6방식을 사용하면 된다.
const express = require('express');
// 아래 import구문으로 변경됨
import { Router } from 'express';
'dev > nodejs' 카테고리의 다른 글
nodejs - address already in use (0) | 2023.07.24 |
---|---|
nodejs - controller/service로 분리, Callback function is not available with promise clients. (0) | 2023.07.23 |
nodejs - post요청 시 body값 확인 (0) | 2023.07.22 |
nodejs - env파일 (0) | 2023.07.22 |
node js - typescript 설정 (0) | 2023.07.20 |