When running node with --experimental-modules flag, the global require object has a main property but its value is always undefined. Is this by design?
// index.js
console.log(require.main === undefined)
$ node index.js
false
md5-cd3c3c19cb796f98abcd31a1dc450df9
$ node --experimental-modules index.js
true
What would you expect it to print? Without --experimental-modules it's the CJS Module object.
I was expecting to be no difference in behavior (using the flag or not).
I thought .js files are considered cjs (by default) even when using --experimental-modules.
p.s. all the other properties of require object have almost identical values.
I thought .js files are considered cjs (by default) even when using --experimental-modules.
Yes, but they don't use the traditional module loader where require.main === process.mainModule.
The new module loader doesn't really have a concept of "main-ness" right now and I don't know if that is something that ought to be fixed. One for @nodejs/modules to consider.
Oh i see, thank you for your quick response.
Maybe this should be solved before esm support drop experimental status, because it will break all cjs modules that rely on require.main
I found this a few weeks ago but wasn't sure how to fix it, I'll take another look before friday.
Agreed this is a bug, had just put on my list to fix too.
@guybedford Is this issue still open?
@vsnehil92 yes, because the linked PR #21150 hasn't yet been merged.
@ljharb The changes have already been approved.
yes, but issues typically stay open til the PR is merged.
https://github.com/nodejs/node/pull/21150 was merged. Closing.