I'm also facing the same issue in a Next.js project.
❌ Problem
When trying to use node-llama-cpp with the following code:
import { getLlama, LlamaChatSession } from "node-llama-cpp";
const llama = await getLlama();
I got this error:
⨯ Error: require() of ES Module ...node_modules\node-llama-cpp\dist\index.js from ...\.next\server\app\api\aa\route.js not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules.
✅ Solution
Since node-llama-cpp is an ES module, it cannot be loaded using require() in a CommonJS context. If you're working in a Next.js API route (which uses CommonJS under the hood), you need to use a dynamic import workaround:
const nlc: typeof import("node-llama-cpp") = await Function(
'return import("node-llama-cpp")',
)();
const { getLlama, LlamaChatSession } = nlc
This approach uses the Function constructor to dynamically import the ES module, bypassing the CommonJS limitation.
📚 Reference
You can find more details in the official node-llama-cpp troubleshooting guide.