unum-cloud / usearch

Fast Open-Source Search & Clustering engine × for Vectors & 🔜 Strings × in C++, C, Python, JavaScript, Rust, Java, Objective-C, Swift, C#, GoLang, and Wolfram 🔍
https://unum-cloud.github.io/usearch/
Apache License 2.0
1.92k stars 109 forks source link

Bug: npm package does not support esm in nodejs project. #426

Open xianminx opened 1 month ago

xianminx commented 1 month ago

Describe the bug

My NodeJS project fails to run when type set to module in package.json. The log shows it is caused by redundant module.exports in usearch.js file.

file:///Users/lucas/dev/workspace/gpt/uphoto/node_modules/.pnpm/usearch@2.11.1/node_modules/usearch/javascript/dist/esm/usearch.js:461
module.exports = {
^

ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/lucas/dev/workspace/gpt/uphoto/packages/server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/lucas/dev/workspace/gpt/uphoto/node_modules/.pnpm/usearch@2.11.1/node_modules/usearch/javascript/dist/esm/usearch.js:461:1
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:475:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5)

Node.js v22.2.0

Steps to reproduce

  1. create a NodeJS project, setting type to be module in package.json.
  2. create a plain index.js.
  3. import usearch in index.js
  4. run node index.js

Expected behavior

I see usearch.js file supports ESM and CommonJS by writing module.exports and export in the same file.

module.exports should not be included in ESM dist.

USearch version

v2.11.1

Operating System

macOS

Hardware architecture

x86

Which interface are you using?

C++ implementation

Contact Details

No response

Are you open to being tagged as a contributor?

Is there an existing issue for this?

Code of Conduct

ashvardanian commented 1 month ago

Interesting, @xianminx! Seems like it will be fixed by #420, right?

johnhorsema commented 1 month ago

In the esm file, I had to replace

module.exports = {
  Index,
  MetricKind,
  ScalarKind,
  Matches,
  BatchMatches,
  exactSearch,
};

with

const usearch = {
    Index,
    MetricKind,
    ScalarKind,
    Matches,
    BatchMatches,
    exactSearch,
};
export default usearch;
ashvardanian commented 1 month ago

@johnhorsema this seems better, than what we've merged into main-dev. If that's so, can you open a PR?