Ollama's JavaScript Library: Chat, Streaming, Browser Use, Custom Hosts and Aborting Requests
Note: Some future links on this site may be affiliate links. We never claim hands-on testing; guides are researched from the public sources listed in each article.
How we wrote this: A summary of the official ollama-js README on GitHub and Ollama's FAQ (linked below), checked October 29, 2026. Code is adapted from the README's examples. The model name
llama3.1is the README's example; use any model you have pulled.
The official JavaScript library wraps Ollama's local API so a Node script, an Electron app or a web page can talk to a model running on your own computer. It's maintained by the Ollama project. (Python user? See Ollama's Python library.)
Install
You'll need Ollama installed and running with at least one model pulled (ollama pull llama3.1), then:
npm i ollama
A first chat call
import ollama from 'ollama'
const response = await ollama.chat({
model: 'llama3.1',
messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})
console.log(response.message.content)
The messages array is the chat history; each entry has a role of 'user', 'system' or 'assistant' and its content. Send the earlier turns back with each request to continue a conversation.
Stream the reply
Set stream: true and the call returns an AsyncGenerator; each part is one piece of the reply:
import ollama from 'ollama'
const response = await ollama.chat({
model: 'llama3.1',
messages: [{ role: 'user', content: 'Why is the sky blue?' }],
stream: true,
})
for await (const part of response) {
process.stdout.write(part.message.content)
}
In a browser, append part.message.content to the page instead of writing to stdout.
Use it in a web page
To use the library without Node, import the browser module:
import ollama from 'ollama/browser'
Browsers enforce cross-origin rules. Ollama's FAQ says it allows cross-origin requests from 127.0.0.1 and 0.0.0.0 by default; a page served from another origin, or a browser extension, needs that origin added with the OLLAMA_ORIGINS environment variable on the server. (How to set server variables on each system is in the FAQ and our Linux, Mac and Windows setup guides.) Allow only the origins you actually use.
Point it at another machine
The default host is http://127.0.0.1:11434. For a server elsewhere on your network, create a client:
import { Ollama } from 'ollama'
const ollama = new Ollama({ host: 'http://127.0.0.1:11434' })
Replace the address with the other machine's. The constructor also accepts headers (added to every request) and a custom fetch implementation. The server has to be listening beyond localhost (OLLAMA_HOST) for this to work.
Useful options on chat
From the README's API reference, all optional:
format: ask for JSON output (see structured outputs)images: attach images to a message for vision modelstools: let the model request function calls (tool calling basics)think: turn reasoning on or off on models that support it (thinking models)keep_alive: how long the model stays loaded, as seconds or a string like"5m"options: runtime settings such as context length (context length explained)
Other calls
| Task | Call |
|---|---|
| One-off completion | ollama.generate({ model, prompt }) |
| Embeddings | ollama.embed({ model, input }), where input can be a string or an array |
| List / loaded models | ollama.list(), ollama.ps() |
| Download / delete | ollama.pull({ model }), ollama.delete({ model }) |
| Build a variant | ollama.create({ model, from, system }) |
| Server version | ollama.version() |
Embeddings power search over your own documents; see embeddings and semantic search.
Stop a runaway answer
ollama.abort() aborts all streamed generations running on that client instance, and every loop reading those streams throws an AbortError. The README recommends one client per stream if you need to cancel streams individually or manage timeouts, so a "Stop" button only stops its own answer:
import { Ollama } from 'ollama'
const client = new Ollama()
// start a streamed chat with client, then on "Stop":
client.abort()
Wrap your for await loop in try/catch to handle the AbortError cleanly.
Local vs. cloud
The README also covers Ollama's cloud models and web search, which need an Ollama account or API key and send requests to Ollama's servers. If privacy is why you're running models locally, use local models only.