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

Prevents package from crashing when import.meta.url is undefined #306

Closed
wants to merge 1 commit into from

Conversation

IronGeek
Copy link

This PR prevents open from crashing when the package is bundled for non-ESM format.
It does so by adding a simple truthy guard for import.meta.url before passing it to fileURLToPath() function.

In non-ESM context, import.meta.url is undefined.

open/index.js

Lines 13 to 15 in 36c61af

// Path to included `xdg-open`.
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const localXdgOpenPath = path.join(__dirname, 'xdg-open');

And since fileURLToPath() does not accept undefined value as argument, it will throw the following error:

node:internal/url:1210
    throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined
...

@sindresorhus
Copy link
Owner

This should be opened on the bundler you use instead. It's up to your bundle to polyfill things if it converts from ESM to non-ESM.

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.

None yet

2 participants