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

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

Closed
Freed-Wu opened this issue Dec 28, 2023 · 5 comments

Comments

@Freed-Wu
Copy link

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
Copy link

ZomoXYZ commented Jan 26, 2024

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
Copy link

@sindresorhus is a fix in sight for this?

@sindresorhus
Copy link
Owner

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

@sindresorhus sindresorhus closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2024
@Freed-Wu
Copy link
Author

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
Copy link

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)",
},

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

No branches or pull requests

4 participants