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
Remove usages of require.main #9771
Conversation
if (require.main !== module) { | ||
throw new Error('This script is designed to be run as the main process'); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was meant as a safely check, but I think it's safe enough to get rid of it 🙃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
// If there is only one argument and `server.js` is being executed directly, | ||
// legacy it into the config option. | ||
if (require.main === module && argv['_'].length === 1) { | ||
argv['config'] = argv['_'][0]; | ||
argv['_'] = []; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully it's safe to break this legacy feature now? We certainly don't rely on it for production deployments, and it's been over 6 years since we introduced --config
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it's fine to break this. Just to make sure I understand, if someone is in fact using the old syntax then it will break with an error message, right? It won't just silently break?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it will in fact silently break; yargs-parser
doesn't do any validation.
All images
|
apps/prairielearn/src/server.js
Outdated
@@ -2165,7 +2158,7 @@ export async function insertDevUser() { | |||
await sqldb.queryAsync(adminSql, { user_id }); | |||
} | |||
|
|||
if (require.main === module && config.startServer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out this is required for our test suite 🙃 I'll add it back for now and figure out something else on the ESM conversion branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dang! So many hacks 😅
This was ultimately rolled into the ESM conversion. |
There is no direct replacement for
require.main
in ESM modules. There's always https://www.npmjs.com/package/es-main... but I'd rather not drag in another dependency when IMO we never actually really need this check.