I am encountering issues while trying to establish a connection with the Chromadb vector database in Nextjs. The objective is to store user-generated content in Chromadb. Below is the code snippet I am utilizing along with its dependencies:
Dependencies Version:
- next: "14.1.0"
- chromadb: "1.8.1"
app/upload/action.tsx:
"use server";
import { ChromaClient } from "chromadb";
export async function createDocs(prevState: any, formData: FormData) {
const client = new ChromaClient({ path: "http://0.0.0.0:8000", });
const collection = await client.getCollection({ name: "demo", });
const response = await collection.add({
ids: ['1'],
documents: [`${formData.get("content")}`],
metadatas: [{ user: 1, title: `${formData.get("title")}` }],
});
return { message:`${response}` };
}
When executing the provided code, I encounter an unusual error in webpack that states:
⨯ ./node_modules/onnxruntime-node/bin/napi-v3/darwin/arm64/onnxruntime_binding.node
Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type, as currently no loaders are configured for processing this file. Visit https://webpack.js.org/concepts#loaders for more information.
(Source code omitted for this binary file)
Import trace for requested module:
./node_modules/onnxruntime-node/bin/napi-v3/darwin/arm64/onnxruntime_binding.node
./node_modules/onnxruntime-node/bin/napi-v3/ sync ^\.\/.*\/.*\/onnxruntime_binding\.node$
./node_modules/onnxruntime-node/dist/binding.js
./node_modules/onnxruntime-node/dist/backend.js
./node_modules/onnxruntime-node/dist/index.js
./node_modules/@xenova/transformers/src/backends/onnx.js
./node_modules/@xenova/transformers/src/env.js
./node_modules/@xenova/transformers/src/transformers.js
./node_modules/chromadb/dist/chromadb.mjs
./app/upload/actions.tsx
The issue arises due to Chromadb experiencing difficulty when using ESM modules in
./node_modules/chromadb/dist/chromadb.mjs
. However, it functions properly with CommonJS.
I have tried multiple troubleshooting steps, despite being aware of their potential inefficacy. These actions include:
- Installing different packages such as npm, yarn, bun, and pnpm.
- Replacing server actions with API routes.
- Attempting to initialize, export, and import the client variable.
file.ts:
import {ChromaClient} from 'chromadb'
const client = new ChromaClient({
path: 'http://0.0.0.0:8000'
});
export async function collection() {
const collection = await client.getCollection({
name: 'demo'
})
}
Is there a method to utilize commonjs in NextJs?