pgvector / pgvector-node

pgvector support for Node.js, Deno, and Bun (and TypeScript)
MIT License
303 stars 9 forks source link

cannot use on cloudflare workers #20

Closed dcsan closed 4 months ago

dcsan commented 4 months ago

when trying to use this extension with a cloudflare worker I run into this error:

✘ [ERROR] Could not resolve "util"

    node_modules/pgvector/src/drizzle-orm/index.js:3:21:
      3 │ const util = require('util');
        ╵                      ~~~~~~

  The package "util" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file and make sure to prefix the module name with "node:" to enable Node.js compatibility.

now cloudflare has implemented these node compatability layer

https://developers.cloudflare.com/workers/runtime-apis/nodejs/util/

but it requires any imports, including in node_module packages to prefix the import with node: ..

https://developers.cloudflare.com/workers/runtime-apis/nodejs/

Node.js APIs are available under the node: prefix, and this prefix must be used when importing modules, both in your code and the npm packages you depend on.

// Do this:
import { Buffer } from 'node:buffer';

// Do not do this:
import { Buffer } from 'buffer';

I was wondering if this is something that could be implemented? how would this work - as a special build for running in the cloudflare env?

dcsan commented 4 months ago

related https://github.com/porsager/postgres?tab=readme-ov-file#cloudflare-workers-support

ankane commented 4 months ago

Hi @dcsan, Cloudflare Workers aren't officially supported, but this makes sense for Node.js and Bun as well, so updated in the commit above.

dcsan commented 4 months ago

awesome, thanks! so until there is a new release, I assume to use this I should directly install from the repo like:

npm i https://github.com/pgvector/pgvector-node

do you have a release process / script in the repo? I don't see anything there.

dcsan commented 4 months ago

oof, now i get this:

✘ [ERROR] service core:user:knowbase-api: Uncaught Error: Dynamic require of "node:util" is not supported

    at null.<anonymous> (index.js:13:9)
    at null.<anonymous> (index.js:10590:17) in node_modules/pgvector/src/drizzle-orm/index.js
    at null.<anonymous> (index.js:19:50) in __require2
    at null.<anonymous> (index.js:81123:34)

and the first line import points to this:

image

in my own code i'm using import syntax:

import { vector } from "pgvector/drizzle-orm"

ankane commented 4 months ago

Removed that require since it's unused, but I'm guessing you'll run into the same error in a different file. I don't fully understand the error message, but you could try applying this diff to see if it fixes it (or produces a different error).

--- a/src/utils/index.js
+++ b/src/utils/index.js
@@ -1,4 +1,4 @@
-const util = require('node:util');
+const { format } = require('node:util');

 function fromSql(value) {
   return value.substring(1, value.length - 1).split(',').map((v) => parseFloat(v));
@@ -17,7 +17,7 @@ function sqlType(dimensions) {
     throw new Error('expected integer');
   }

-  return util.format('vector(%d)', dimensions);
+  return format('vector(%d)', dimensions);
 }

 module.exports = {fromSql, toSql, sqlType};