You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to determine whether a file has been run directly inside ES module #2065
When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module.
the concept of a main is mostly considered a mistake, so we haven't exposed such a thing to esm. It's generally considered best practice to have separate files for your bin and lib functionality.
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
Activity
devsnek commentedon Jul 19, 2019
the concept of a main is mostly considered a mistake, so we haven't exposed such a thing to esm. It's generally considered best practice to have separate files for your bin and lib functionality.
gireeshpunathil commentedon Dec 21, 2019
ping @sosoba
PoojaDurgad commentedon Sep 16, 2020
@sosoba , were you able to follow @devsnek 's suggestion? or it is still an issue?
sosoba commentedon Sep 16, 2020
Hi @PoojaDurgad.
Temporary I using:
but finally community proposing
import.meta.mainwhich looking god.Please merge with nodejs/node#49440
PoojaDurgad commentedon Sep 19, 2020
@sosoba , thanks for the information. as nodejs/node#49440 is still open and under discussion, let us wait to its resolution
github-actions commentedon Oct 27, 2024
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
github-actions commentedon Nov 26, 2024
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment.
If you need further assistance or have questions, you can also search for similar issues on Stack Overflow.
Make sure to look at the README file for the most updated links.
sosoba commentedon Aug 12, 2025
It works now! nodejs/node#57804