This is just a simple object literal with the property name matching the value:
let ___test = 'this is a string... could be a path';
let object = {___test};
console.log(object);
Using module.exports
, you've exported an object. When importing using the module system, you essentially have a module scoped object, expose
. You then destructure that object's __dirname
property when defining {__dirname}
. I haven't tested it, but you should be able to just import __dirname
as a named import like so (though you'd likely run into a conflict for the variable name):
import __dirname from './expose.js';
EDIT: Did some testing, when you do the above import, you get your exported object, so you would still need to destructure after importing (and of course, destructure into a new variable):
import __dirname from './expose.js';
const {__dirname: dirname} = __dirname;
console.log(dirname);
After some googling, it appears you cannot destructure an object when performing an ES6 import. The syntax appears similar to destructuring an object but it is not. What you are doing when you do the following is actually import variables that have been exported as named exports.
test.mjs
export const __dirname = 'this is a path';
test-import.mjs
import { __dirname } from './test.mjs';
console.log(__dirname);
Note that I didn't use export const __dirname = __dirname;
, or even export const dirname = __dirname;
. This is because node specific globals aren't available in .mjs
files currently. So, in your example, expose.js
is still a standard .js file which has access to both the module
and __dirname
globals. The __dirname
value is then exported as a property of the default commonjs export object (i.e. module.exports = {__dirname}
), which is then made available as the default export when importing in your use.mjs
file. The expose
variable is the module.exports object literal that was exported. Once it's been initialized as an imported variable, you can then destructure out the __dirname value.
Hope this helps.
These other sources were helpful for me in understanding this as well: Destructuring a default export object
https://github.com/babel/babel-loader/issues/194#issuecomment-168579479
https://github.com/nodejs/node/issues/16844#issuecomment-342245465