ERROR

[ Error ] - ReferenceError: __dirname is not defined in ES module scope

algml0703 2022. 8. 15. 16:12
반응형

설정을 module로 해두고 아래와 같이 작업 중 에러가 발생하였다.

import path from 'path';
const directoryPath = path.join(__dirname, "../router/api");

위의 에러는 es 모듈 환경에서는 __dirname이 존재하지 않기 때문에 발생하는 문제이다. 이를 해결할 수 있는 두가지 방안이 있다.

1. 

import path from 'path';

import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const directoryPath = path.join(__dirname, "../router/api");

2.

import path from 'path';
const directoryPath = path.join(__dirname, "../router/api");
const __dirname = path.resolve();

 

출처

https://errorsandanswers.com/__dirname-is-not-defined-in-node-14-version/

 

반응형