sindresorhus / open

Open stuff like URLs, files, executables. Cross-platform.
MIT License
3.14k stars 213 forks source link

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

Closed IronGeek closed 1 year ago

IronGeek commented 1 year ago

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. https://github.com/sindresorhus/open/blob/36c61af1e696ef4365fd12dcc733586877106f19/index.js#L13-L15

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 commented 1 year ago

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.