Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require.main is undefined when using --experimental-modules #21143

Closed
mgalakos opened this issue Jun 5, 2018 · 11 comments
Closed

require.main is undefined when using --experimental-modules #21143

mgalakos opened this issue Jun 5, 2018 · 11 comments
Assignees
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. question Issues that look for answers.

Comments

@mgalakos
Copy link

mgalakos commented Jun 5, 2018

  • Version: node v10.3.0
  • Platform: linux, windows, wsl

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
$ node --experimental-modules index.js
true
@bnoordhuis bnoordhuis added question Issues that look for answers. esm Issues and PRs related to the ECMAScript Modules implementation. labels Jun 5, 2018
@bnoordhuis
Copy link
Member

What would you expect it to print? Without --experimental-modules it's the CJS Module object.

@mgalakos
Copy link
Author

mgalakos commented Jun 5, 2018

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.

@mgalakos mgalakos closed this as completed Jun 5, 2018
@mgalakos mgalakos reopened this Jun 5, 2018
@bnoordhuis
Copy link
Member

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.

@mgalakos
Copy link
Author

mgalakos commented Jun 5, 2018

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

@mgalakos mgalakos closed this as completed Jun 5, 2018
@devsnek devsnek reopened this Jun 5, 2018
@devsnek devsnek self-assigned this Jun 5, 2018
@devsnek devsnek added the confirmed-bug Issues with confirmed bugs. label Jun 5, 2018
@devsnek
Copy link
Member

devsnek commented Jun 5, 2018

I found this a few weeks ago but wasn't sure how to fix it, I'll take another look before friday.

@guybedford
Copy link
Contributor

Agreed this is a bug, had just put on my list to fix too.

@vsnehil92
Copy link
Contributor

@guybedford Is this issue still open?

@ljharb
Copy link
Member

ljharb commented Jun 6, 2018

@vsnehil92 yes, because the linked PR #21150 hasn't yet been merged.

@vsnehil92
Copy link
Contributor

@ljharb The changes have already been approved.

@ljharb
Copy link
Member

ljharb commented Jun 6, 2018

yes, but issues typically stay open til the PR is merged.

@targos
Copy link
Member

targos commented Jun 13, 2018

#21150 was merged. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. esm Issues and PRs related to the ECMAScript Modules implementation. question Issues that look for answers.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants