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

Use process.argv[1] as a fallback for new NodePackageImporter() #275

Merged
merged 2 commits into from
Feb 21, 2024

Conversation

nex3
Copy link
Contributor

@nex3 nex3 commented Feb 20, 2024

Closes #274

@nex3 nex3 requested a review from jathak February 20, 2024 23:50
@@ -21,6 +22,10 @@ export class NodePackageImporter {
? p.resolve(entryPointDirectory)
: require.main?.filename
? p.dirname(require.main.filename)
// TODO: Find a way to use `import.meta.main` once
// https://github.com/nodejs/node/issues/49440 is done.
: process.argv[1]
Copy link
Contributor

@ntkme ntkme Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small risk here is that process.argv is mutable and can be mutated before this is executed. Otherwise this would be a pretty good workaround for ESM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can avoid that issue here. At least if users run into it, they always have the option of passing something in explicitly to work around it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand the concern correctly, this also applies to require.main.filename. We take advantage of that for testing different scenarios for CommonJS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the chance of argv getting modified would be higher as it’s pretty common to do something like arg = argv.shift() when parsing arguments.

@nex3 nex3 merged commit e6be3a3 into main Feb 21, 2024
16 checks passed
@nex3 nex3 deleted the pkg-importer-esm branch February 21, 2024 01:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Node.js Package Importer shouldn't be strict about require.main.filename
4 participants