pirple / The-NodeJS-Master-Class

Code samples for the Node.js Master Class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReferenceError: __dirname is not defined

dcjayasuriya2020 opened this issue · comments

code in the datafile is lib.baseDir = path.join(__dirname, '/../.data/');
in git bash it indicates when run for test err,

$ node index.js
file:///E:/Learn/lib/data.js:15
lib.baseDir = path.join(__dirname, '/../.data/');
                        ^

 ReferenceError: __dirname is not defined

How to get clear on this?

commented

const __dirname = path.resolve();

import path from 'path';
import { fileURLToPath } from 'url';
// data path
const __filename = fileURLToPath( import.meta.url );
const __dirname = path.dirname( __filename );
//* Base directory of the data folder
const templateDir = path.join( __dirname, '/../.data/' );