Having trouble connecting Nextjs with ChromaDB?

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?

Answer №1

The issue stemmed from a problem with Webpack. Specifically, the error occurred while Webpack was attempting to process a binary file named onnxruntime_binding.node, which is a component of the onnxruntime-node package.

To solve this issue, we can tweak some settings in Webpack as follows:

1. Adding node-loader

The node-loader tool is designed to handle .node files, which should help resolve the problem related to onnxruntime_binding.node.

You can easily install it using any preferred package manager as a Dev dependency:

npm install node-loader --save-dev

2. Configuring Webpack

Add the following code snippet to your next.config.mjs file:

const nextConfig = {
  webpack: (config, { isServer }) => {
    if (!isServer) {
      // Fixes npm packages that rely on the `fs` module
      config.resolve.fallback.fs = false;
    }

    config.module.rules.push({
      test: /\.node$/,
      use: 'node-loader',
    });

    return config;
  },
};

export default nextConfig;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The output returned by an AngularJS controller

John Papa introduced the 'controller as' technique for AngularJS in his article titled Do You Like Your Angular Controllers with or without Sugar: myApp.controller("MainCtrl", [ function () { var vm = this; // using ViewModel conv ...

Using Buttons to Filter Data in React

My goal is to implement button functionality that filters data from a JSON file. Upon clicking a button, the state should be updated with the filtered JSON data and display the list with the updated information. Currently, I have four buttons for filteri ...

Tips on accessing the v-model value with a parameter in VUE

Looking to access the v-model value using a parameter, I attempted the following code: <template> <div v-for="(item, idx) in data"> <input :id="item" :v-model="item"></input> <button @click=&q ...

Is there a neat method in React and Material UI for de-structuring the props that I am passing to useStyles?

When passing props to useStyles based on the Material docs, our code looks like this: const useStyles = makeStyles({ // style rule foo: props => ({ backgroundColor: props.backgroundColor, }), bar: { // CSS property color: props => ...

Using Jquery to detect if there are any Space characters in the user input

In my form, users are required to set up a new Username. The problem arises when they include a space in their username, which I want to prevent. Currently, I am able to detect the presence of a space with this code: var hasSpace = $('#usernameValue ...

Restrict the duplication of div elements with JQuery

Here is the structure I'm working with: <div class="checkmark-outer"> <div class="checkmark-33"> <div class="fa-stack fa-1x checkmark-icon"> <i class="fa fa-circle fa-stack-2x icon-background"></i> ...

What is the significance of `()=>` in JavaScript when using Selenium?

What is the significance of () => in the following sentence? ()=>{Object.defineProperties(navigator,{webdriver:{get:()=>false}})} I have only seen this syntax in JavaScript and it seems to be used for configuring page evaluation with Selenium. T ...

Is there a way to transfer the input value from a textfield in one component to another component in ReactJS?

I have a scenario where I need to pass the value of a text area from one component in reactjs to another. The component's value is stored using a useState hook in the first component, and I want to access it in another component to run a map() functio ...

Tips on converting a Java regular expression to JavaScript regular expression

Can someone assist me in translating the Java Regex code below to JavaScript Regex? (\\\p{Upper}{2})(\\\d{2})([\\\p{Upper}\\\p{Digit}]{1,30}+) I attempted using the following JavaScript Regex: ...

Troubleshooting problem with resizing and links in IE11 for iframe-resizer

When using the iframe-resizer from https://github.com/davidjbradshaw/iframe-resizer, I have encountered a few issues. Upon resizing the browser window, I noticed that if I first restore down and then manually resize, some extra padding appears. The paddin ...

I am looking to personalize a Material UI button within a class component using TypeScript in Material UI v4. Can you provide guidance on how to achieve this customization?

const styling = { base: { background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)', border: 0, borderRadius: 3, boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)', color: 'white', height: 48, ...

Solving the issue where the argument type is not assignable to the parameter type

I am attempting to filter an array of objects in React using TypeScript and encountered this error. Below is my interface, state, and function: TS2345: Argument of type '(prev: IBudget, current: IBudget) => IBudget | undefined' is not assigna ...

When using html2canvas in Angular, it is not possible to call an expression that does not have a call signature

I'm currently working on integrating the html2canvas library into an Angular 8 project. Despite trying to install the html2canvas types using npm install --save @types/html2canvas, I'm still facing issues with its functionality. Here's how ...

loading preloaded fonts at the optimal moment

My webfonts are loaded using Webfontloader as shown below: <script src="//ajax.googleapis.com/ajax/libs/webfont/1.5.10/webfont.js"></script> <script> WebFont.load({ custom: { families: ['BlenderProBook' ...

Struggling to make an AJAX form function properly

I'm running into an issue while setting up a basic AJAX form. My goal is to have a message display on the same page upon successful login using a PHP form through AJAX, but currently, I get redirected to the PHP file after form submission. Can anyone ...

Update the HTML page when switching between tabs

Currently, I am experiencing an issue with tab navigation in HTML. Within my code, I have two tabs named #tab1 and #tab2, each containing a form. The problem arises when I input data into #tab1, switch to #tab2, and then return to #tab1 - the information I ...

Initializing Three.js to load the model

I have a 3D model that I initially had in the 3DS format. I then converted it to OBJ and finally to JS format. Now, my goal is to load this model into another JS file. Below you'll find the code snippet I've written for this purpose: var loader ...

Implementing Knockout.js with JqueryUI Autocomplete: Access the complete object instead of just the value

I have implemented a custom binding for a JQueryUI auto complete feature that works well. However, I am looking to modify it so that it returns the Item object, which can then be pushed to another array. Can someone provide guidance on how to achieve this ...

Halt the update of a variable when a different variable reaches zero

I am currently working on a jQuery script that updates variables based on scroll events. var scrollTop = $(window).scrollTop(); var width = site.logoWidth - scrollTop; var logoPadding = scrollTop; My goal is to prevent the logoPadding variable from updat ...

How can one dynamically update a page in Angular when the path is changed?

I am facing a pagination issue in Angular. Here is my HTML code: <!-- A div element for pagination part at the bottom of the page --> <div style="margin-left: 50%; margin-top: 20px; margin-bottom: 20px"> <ul class="paginat ...