Webpack으로 React 개발 환경을 구축하면서 다음과 같은 에러를 맞닥뜨리게 되었다.
ReferenceError: __dirname is not defined
해당 문제는 package.json에 type: "module" 설정을 추가하여 CommonJS 모듈이 아닌 ES6 모듈을 사용하게 되는 경우 발생한다.
ES6 모듈에는 __dirname 변수가 존재하지 않기 때문에 별도로 정의가 필요하다.
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
위와 같이 정의하고 __dirname 변수를 사용하면 정상적으로 경로를 불러오는 것을 확인할 수 있다.
'개발 > 나의 오류 일지' 카테고리의 다른 글
[나의 오류 일지] styled-components 글자 깜빡임 현상 (FOUT) (0) | 2024.08.04 |
---|---|
[나의 오류 일지] MIME type ('text/html') is not executable (0) | 2024.07.28 |
[나의 오류 일지] Typescript emitted no output (0) | 2024.07.14 |