Node.js에서 파일 입출력 fs 모듈 사용시 발생 에러

발생 에러

Node.js에서 fs 모듈을 불러오려고 할 때,
ReferenceError: require is not defined in ES module scope, you can use import instead 에러가 발생했다.

const fs = require("fs");
           ^

ReferenceError: require is not defined in ES module scope, you can use import instead


이유

ES 모듈 시스템을 사용하는 프로젝트에서 require을 사용해 모듈을 불러오려고 했기 때문에 발생하는 에러다.
require 키워드는 CommonJS 모듈 시스템을 사용하는 프로젝트에서 사용하는 키워드이다.


해결 방법

ES 모듈에서는 import 키워드를 사용해서 fs모듈을 불러온다.

// ES Modules
import fs from "fs";

Categories:

Updated:

Leave a comment