Reputation: 251
I'm working on a Node.js project where I'm trying to use the ollama (ollama-js) module. However, when I call the async function chatWithLlama()
(which has ollama.chat()
inside), I encounter the following error:
TypeError: ollama.chat is not a function
And the chatWithLlama()
function doesn't finish off.
Here's the relevant code snippet:
const ollama = require('ollama');
async function chatWithLlama() {
try {
const response = await ollama.chat({
model: 'llama3',
messages: [{ role: 'user', content: 'Why is the sky blue?' }],
});
console.log(response.message.content);
} catch (error) {
console.error('Error:', error);
}
}
chatWithLlama();
For more context, this is my package.json:
{
"name": "nodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@api/pplx": "file:.api/apis/pplx",
"@types/node": "^18.0.6",
"ollama": "^0.5.0"
}
}
I have made sure that the ollama module is properly installed using npm and included in the package.json file and node_modules directory. I also have Ollama running on my Mac, with llama3 already installed. Nothing other than this error occurs, and the script does not crash. What's causing this issue and how can I resolve it?
Upvotes: 4
Views: 2162
Reputation: 3903
In ECMAScript modules (ESM), this 2 basically represents the same thing:
import ollama from 'ollama';
import { default as ollama } from 'ollama';
However, when you write the same thing in CommonJS (CJS) (require()
is a CJS only pattern), it is the equivalent of namespace import in ESM:
const ollama = require('ollama'); // CJS
import * as ollama from 'ollama'; // ESM
Therefore, in order to solve this issue, you should read it from an export named default
instead. Here's a translation between default import in CJS and ESM:
import ollama from 'ollama'; // ESM
const { default: ollama } = require('ollama'); // CJS
The above answer is just a very simple overview on what happened. There's a lot of details about this and some additional reading that can be done include:
__esModule
: What's the purpose of `Object.defineProperty(exports, "__esModule", { value: !0 })`?Upvotes: 5