sindresorhus / open

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

UnhandledRejection: The "path" argument must be of type string or an instance of URL. Received undefined #330

Closed Freed-Wu closed 6 months ago

Freed-Wu commented 9 months ago
import open from 'open';

//...
    this.addAction('open', async (item: ListItem) => {
      let status = 'succeed';
      try {
        await open(item.data.url);
      } catch (e) {
        status = 'failed';
      }
      window.showMessage(`${status} to open ${item.data.url}`);
    });

After yarn build, I test and get:

UnhandledRejection: The "path" argument must be of type string or an instance of URL. Received undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined
    at fileURLToPath (node:internal/url:1399:11)
    at Object.<anonymous> (/the/path/lib/index.js:3627:84)

So I search /the/path/lib/index.js:3627:84, and find:

// node_modules/open/index.js
var import_meta = {};
var __dirname = import_node_path.default.dirname((0, import_node_url.fileURLToPath)(import_meta.url));

The last line is 3627. It looks like a bug about node_modules/open/index.js, So I report it.

ZomoXYZ commented 8 months ago

Removing the const __dirname = path.dirname(fileURLToPath(import.meta.url)); line from index.js in my node_modules folder temporarily fixed this for me since the __dirname variable is included with node by default. I found a similar issue on another repo and there the fix was to use __dirname instead of import.meta.url.

DavidKirn commented 6 months ago

@sindresorhus is a fix in sight for this?

sindresorhus commented 6 months ago

The problem is with people's bundler config, not this package. There is nothing to fix here.

Freed-Wu commented 6 months ago

The problem is with people's bundler config

This is my esbuild.mjs. Can you help me find the problem? TIA!

import fs from "fs";
import * as esbuild from "esbuild";

const loadJSON = (path) =>
  JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const data = loadJSON("package.json");

const options = {
  entryPoints: ["src/index.ts"],
  bundle: true,
  minify: process.env.NODE_ENV === "production",
  sourcemap: process.env.NODE_ENV === "development",
  mainFields: ["module", "main"],
  external: ["coc.nvim"],
  platform: "node",
  target: "node16",
  outfile: data.main,
};

if (process.argv.length > 2 && process.argv[2] === "--watch") {
  const ctx = await esbuild.context(options);
  await ctx.watch();
  console.log("watching...");
} else {
  const result = await esbuild.build(options);
  if (result.errors.length) {
    console.error(result.errors);
  }
}
DavidKirn commented 5 months ago

The problem is with people's bundler config

This is my esbuild.mjs. Can you help me find the problem? TIA!

import fs from "fs";
import * as esbuild from "esbuild";

const loadJSON = (path) =>
  JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const data = loadJSON("package.json");

const options = {
  entryPoints: ["src/index.ts"],
  bundle: true,
  minify: process.env.NODE_ENV === "production",
  sourcemap: process.env.NODE_ENV === "development",
  mainFields: ["module", "main"],
  external: ["coc.nvim"],
  platform: "node",
  target: "node16",
  outfile: data.main,
};

if (process.argv.length > 2 && process.argv[2] === "--watch") {
  const ctx = await esbuild.context(options);
  await ctx.watch();
  console.log("watching...");
} else {
  const result = await esbuild.build(options);
  if (result.errors.length) {
    console.error(result.errors);
  }
}

I was able to fix it by adding the following lines to my esbuild options:

define: { 'import.meta.url': '_importMetaUrl' },
banner: {
    js: "const _importMetaUrl=require('url').pathToFileURL(__filename)",
},