xenova / transformers.js

State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
https://huggingface.co/docs/transformers.js
Apache License 2.0
11.27k stars 702 forks source link

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

Open logancyang opened 8 months ago

logancyang commented 8 months ago

System Info

transformers.js version: 2.14.0 MacOS Sonoma 14.2.1 NodeJS: v19.8.1 React: v18.2.0

Environment/Platform

Description

Hi I'm trying to follow this instruction at LangchainJS to use the huggingface transformers embeddings in my Obsidian plugin copilot, but am seeing an error regarding this package, not sure how to fix, any help is greatly appreciated! Let me know if anything else is needed to debug this.

Reproduction

Issue

// node_modules/@xenova/transformers/src/env.js
var import_meta = {};
var { env: onnx_env } = ONNX;
var VERSION2 = "2.14.0";
var WEB_CACHE_AVAILABLE = typeof self !== "undefined" && "caches" in self;
var FS_AVAILABLE = !isEmpty(import_fs.default);
var PATH_AVAILABLE = !isEmpty(import_path.default);
var RUNNING_LOCALLY = FS_AVAILABLE && PATH_AVAILABLE;
var __dirname = RUNNING_LOCALLY ? import_path.default.dirname(import_path.default.dirname(import_url.default.fileURLToPath(import_meta.url))) : "./";

Error:
Plugin failure: copilot TypeError: The "path" argument must be of type string or an instance of URL. Received undefined
    at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
    at new NodeError (node:internal/errors:399:5)
    at Object.fileURLToPath (node:internal/url:1491:11)
    at anonymous (plugin:copilot:89293:110)
    at e.<anonymous> (app.js:1:2026677)
    at app.js:1:237258
    at Object.next (app.js:1:237363)
    at a (app.js:1:236081)

Additional info

Obsidian uses esbuild and my config is:

import builtins from "builtin-modules";
import esbuild from "esbuild";
import svgPlugin from "esbuild-plugin-svg";
import process from "process";
import wasmPlugin from "./wasmPlugin.mjs";

const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;

const prod = (process.argv[2] === "production");

const context = await esbuild.context({
  banner: {
    js: banner,
  },
  entryPoints: ["src/main.ts"],
  bundle: true,
  external: [
    "obsidian",
    "electron",
    "@codemirror/autocomplete",
    "@codemirror/collab",
    "@codemirror/commands",
    "@codemirror/language",
    "@codemirror/lint",
    "@codemirror/search",
    "@codemirror/state",
    "@codemirror/view",
    "@lezer/common",
    "@lezer/highlight",
    "@lezer/lr",
    ...builtins],
  format: "cjs",
  target: "es2020",
  logLevel: "info",
  sourcemap: prod ? false : "inline",
  treeShaking: true,
  outfile: "main.js",
  plugins: [svgPlugin(), wasmPlugin],
});

if (prod) {
  await context.rebuild();
  process.exit(0);
} else {
  await context.watch();
}
hiepxanh commented 8 months ago

console log your variable: var __dirname = RUNNING_LOCALLY ? import_path.default.dirname(import_path.default.dirname(import_url.default.fileURLToPath(import_meta.url))) : "./";

this __dirname not return string or URL instance, look like it return undefined

Plugin failure: copilot TypeError: The "path" argument must be of type string or an instance of URL. Received undefined

logancyang commented 8 months ago

@hiepxanh the stacktrace I posted shows Object.fileURLToPath got undefined as argument, so import_meta.url is undefined. A couple of lines above there's import_meta = {} so there's ofc no url. Not sure what to make of this.

hiepxanh commented 8 months ago

do you use ESM?

logancyang commented 8 months ago

do you use ESM?

Just confirmed with the Obsidian community, ESM is not an option right now, all Obsidian plugin must be CJS. I wonder what I can do here 🫠 Hopefully the good people here can find a workaround

SCR-20240123-qnmg
hiepxanh commented 8 months ago

@logancyang you cannot do anything, I have to refactor my code to ESM and decided to give up and use API haha