OK, in Node.js, when using module.exports you'd use:
__dirname
To obtain the location of the current file.
For example, if you ran it from x.js in folder y, you'd get something like this:
blah/blah/blah/y
Now, I can't use, process.cwd because returns the directory of the "root" script, and because I'm using import/export, I have to settle for this hacky method:
// jshint -W024
import path from "path";
import url from "url";
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
NOTE: the -W024, ignore code prevents JSHint from raising an "Expected an identifier and instead saw 'import' (a reserved word)" warning.
Also, I can't modularise it, using:
export default __dirname;
Because it'll return the directory of the module exporting __dirname.
Converting it to a function, like this:
// jshint -W024
import path from "path";
import url from "url";
function __dirname() {
return path.dirname(url.fileURLToPath(import.meta.url));
}
export default __dirname;
Doesn't work either, it'll still return the directory of the module exporting the __dirname variable.
My question is, how do I obtain the directory of the current module in Node.js without having to use __dirname or import.meta.url?
Thank you.