pirple / The-NodeJS-Master-Class

Code samples for the Node.js Master Class
1.32k stars 1.21k forks source link

ReferenceError: __dirname is not defined #24

Open dcjayasuriya2020 opened 3 years ago

dcjayasuriya2020 commented 3 years ago

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?

xuych commented 3 years ago

const __dirname = path.resolve();

Luis-Enrique-Mora commented 2 years ago

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/' );