Ollama JavaScript Library
The Ollama JavaScript library (npm package ollama) is the official client for Node.js and browsers. Its API mirrors the Python library, so the concepts transfer directly.
Install
npm install ollamaadded 2 packages in 1s
Chat in Node.js
Save as chat.mjs (ES modules allow top-level await) and run node chat.mjs:
import ollama from 'ollama';
const response = await ollama.chat({
model: 'llama3.2',
messages: [{ role: 'user', content: 'Give me a name for a pet turtle.' }],
});
console.log(response.message.content);How about "Sheldon"? It's a classic shell pun! (Sample response.)
Streaming with for await
import ollama from 'ollama';
const stream = await ollama.chat({
model: 'llama3.2',
messages: [{ role: 'user', content: 'List three JavaScript array methods.' }],
stream: true,
});
for await (const part of stream) {
process.stdout.write(part.message.content);
}1. map() 2. filter() 3. reduce() (Sample response.)
Custom Host
import { Ollama } from 'ollama';
const client = new Ollama({ host: 'http://127.0.0.1:11434' });
const { models } = await client.list();
console.log(models.map(m => m.name));[ 'llama3.2:latest', 'nomic-embed-text:latest' ]
Browser Usage
Import from "ollama/browser" in front-end code. The browser must be allowed by CORS: localhost origins work by default, other origins need OLLAMA_ORIGINS. Never expose your Ollama server publicly just so a website can call it; route requests through your own backend instead.
Frequently asked questions
Does the Ollama JS library work with TypeScript?
Yes, it ships with type definitions.
How do I abort a streaming request?
Call ollama.abort() or the abort() method on the returned stream to stop all or one in-flight stream.
Can I use Ollama in Next.js?
Yes. Call it from server code such as route handlers or server actions, then stream the result to the client.