sindresorhus / open

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

v9 fails within VSCode extension as import.meta.url is null #324

Open tomqwpl opened 11 months ago

tomqwpl commented 11 months ago

I'm by no means a node expert, so I don't know whether import.meta.url being null is unexpected or not. Trying to use this within a VSCode extension. The "import" of open fails, with an error on the line:

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Error is:

TypeError: The "path" argument must be of type string or an instance of URL. Received undefined
    at new NodeError (node:internal/errors:399:5)
    at fileURLToPath (node:internal/url:1491:11)

I don't know what is unusual about how vscode is loading modules. Not had any issues previously.

The v8.4.2 works in this regard. However, with that version you don't appear to be able to open "file:" urls without explicitly specifying that it should use a browser (using app: { name: apps.chrome} for example). However, that version doesn't have the ability to use the default browser.

tomqwpl commented 11 months ago

Note, I should say that I'm testing on Windows. That is, "file:" urls not opening seems to be a function of Windows behaviour. If you use a Windows command line and just type "Start http://www.google.com" it works, but if you type "start file:///c%3a/somewhere" it doesn't. Note that "start file:///c:/somewhere" DOES work, however, that's not what you get when you URL encode a filename. Anyway, workaround is to explicitly specify "app: browser", but that doesn't work on version 8, "browser" was only introduced in version 9. The reference to "import.meta.url" in version 9 then fails when run inside VSCode for some reason.

tomqwpl commented 11 months ago

Also, fragments don't work with file: URLs on Windows without using a specific app (so app: {name: apps.browser} for example). So a URL such as file:///c:/somewhere#anchor1 appears to lose its fragment with you open it from a windows command line with Start, the address in the browser address bar is just "file:///c:/somewhere". Specifying app: {name: apps.browser} makes this work. All in all, the ability to use "apps.browser" is very convenient, hence why I'm keen to use v9 and not just use v8 as a workaround.