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
相关产品推荐
相关产品推荐

