I use the flag --experimental-modules
when running my node application in order to use ES6 modules.
However when I use this flag the metavariable
As Geoff pointed out the following code returns not the module's path but working directory.
import path from 'path';
const __dirname = path.resolve();
works with --experimental-modules
There have been proposals about exposing these variables through import.meta
, but for now, you need a hacky workaround that I found here:
// expose.js
module.exports = {__dirname};
// use.mjs
import expose from './expose.js';
const {__dirname} = expose;
process.cwd()
From documentation:
The
process.cwd()
method returns the current working directory of the Node.js process.