You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js集成ChromaDB遇Webpack错误,求CommonJS适配方案

Next.js 14连接ChromaDB 1.8.1时的Webpack二进制文件解析问题

依赖版本

  • 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}` }; 
} 

运行时Webpack错误

⨯ ./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, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(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

问题背景与已尝试方案

ChromaDB使用ESM模块时存在该问题,但CommonJS可正常运行。已尝试以下操作,均未解决:

  • 切换包管理器
  • 替换为API路由
  • 调整client初始化方式,代码如下:
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'
  })
}

咨询问题

是否有办法在Next.js中使用CommonJS来解决该问题?

内容的提问来源于stack exchange,提问作者jexroid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 13:15:56