ollama / ollama-js

Ollama JavaScript library

Home Page:https://ollama.ai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error [ERR_REQUIRE_ESM]: require() of ES Module

laport-n opened this issue · comments

Hello,

I'm working with a NestJs framework and tried to use the ollama package.
Unfortunately I'm facing a weird error :

C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\users\auth\auth.controller.js:23
const ollama_1 = __importDefault(require("ollama")); 
^Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Nicolas\Documents\working\feed\automate-ai\node_modules\.pnpm\ollama@0.4.8\no de_modules\ollama\dist\index.js from C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\users\auth\auth.controller.js not suppor ted.
Instead change the require of index.js in C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\users\auth\auth.controller.js to a  dynamic import() which is available in all CommonJS modules.
at Object. (C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\users\auth\auth.controller.js:23:34)
at Object. (C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\users\auth\auth.module.js:12:27)
at Object. (C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\app.module.js:29:23) 
at Object. (C:\Users\Nicolas\Documents\working\feed\automate-ai\dist\main.js:7:22)

I have tried

// doc example
import ollama from "ollama";

// direct class import
import {Ollama} from "ollama";

// dynamic import
const ollamaModule = await import('ollama');
const ollama = ollamaModule.default;

I had the same issue. Issue #44 refers to this problem as well. NestJS compiles down to CommonJS not ES Modules.
Me and another person (I didn't look at theirs before making mine) have a PR for this.
My PR: #60

If you want to use this now, you can checkout my fork npm i, npm run build, and run npm pack to create a .tgz file; copy that to your project and bring in to your project locally... "dependencies": { "ollama": "./ollama-0.0.0.tgz" } don't forget to reinstall packages for your project so the compressed file gets extracted into the node_modules folder.
My Fork: https://github.com/nightness/ollama-js

I have the same problem