본 포스트는 Node.js 에서 모듈 시스템 방식을 ES 방식으로 사용할 경우 __dirname을 찾을 수 없는 오류에 대한 설명입니다.

 

Node.js와 express로 서버를 구성할 때 html 등 Static resources에 대한 경로 설정을 아래와 같이 합니다.

const express = require('express');
const path = require('path');

const app = express();

app.use(express.static(path.join(__dirname, '../public')));
app.get('/', (req,res) => {
    res.sendFile(path.join(__dirname, '../public/index.html'));
});

 

위 소스코드는 static resources는 /public 하위 경로에 두고 초기 화면을 index.html로 설정한 경우이며, 서버를 구동하고 localhost 로 접속하면 /public/index.html을 응답합니다.

 

모듈 시스템을 CommonJS가 아닌 ES 방식을 사용해서 구성하면 require 되신 import ~ from을 사용하여 아래와 같이 작성할 수 있습니다.

import express from "express";
import path from "path";

const app = express();

app.use(express.static(path.join(__dirname, '../public')));
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '../public/index.html'));
});

 

 

위와 같이 작성한 후 서버를 구동해 보면 아래와 같은 오류가 표시됩니다.

ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/a20201022/Documents/expressServer/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/a20201022/Documents/expressServer/src/app.js:7:34
    at ModuleJob.run (node:internal/modules/esm/module_job:185:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)
error Command failed with exit code 1.

 

오류 메시지 그대로 __dirname 이라는 전역 변수는 ES 모듈에서는 선언되지 않아 사용할 수 없다는 의미입니다. 해결을 위해서는 해당 변수를 아래와 같이 선언합니다.

import express from "express";
import path from "path";

const app = express();
//__dirname 선언
const __dirname = path.resolve();

app.use(express.static(path.join(__dirname, '../public')));
app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, '../public/index.html'));
});

 

위와 같이 작성한 후 서버를 구동해 보면 정상 구동됨을 확인할 수 있습니다.

300x250

+ Recent posts