If you build tools in JavaScript or TypeScript, you don't need a cloud API to add AI features. Ollama's official JavaScript library lets a Node.js script, Electron app or web front end send prompts to models running on your own graphics card. This guide follows the library's official README.
What you need
Step 1: Install the library
In your project folder, run:
npm i ollama
The examples below use JavaScript modules (import syntax), so either name your file with a .mjs extension or add "type": "module" to your package.json.
Step 2: Send your first prompt
Create ask.mjs:
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)
Run it with node ask.mjs. The messages array holds the conversation; each message has a role (user, system or assistant) and content. To hold a multi-turn conversation, add the model's reply and your next message to the array and call chat again.
Step 3: Stream the reply
For a chat-style interface, you'll want words to appear as they're generated. Set stream: true and the call returns an async generator:
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)
}
To stop a long reply, the library's ollama.abort() method aborts all streamed generations on that client; the README notes that listening loops then throw an AbortError, and recommends one client per stream if you need per-stream timeouts.
Step 4: Manage models from code
The library mirrors Ollama's REST API, so you can do most of what the command line does:
ollama.list() lists the models you've downloaded.
ollama.pull({ model: 'name' }) downloads a model.
ollama.ps() shows which models are currently loaded.
ollama.show(), ollama.delete() and ollama.copy() inspect, remove and duplicate models.
ollama.embed() generates embeddings, useful for search and retrieval projects.
For background on keeping models in memory between calls, see how to keep Ollama models loaded.
Step 5: Use it in the browser
For front-end code that runs without Node.js, the README says to import the browser module instead:
import ollama from 'ollama/browser'
Requests from a web page follow the Ollama server's normal browser security (CORS) rules. Ollama's FAQ says it allows cross-origin requests from 127.0.0.1 and 0.0.0.0 by default, and you can allow other origins with the OLLAMA_ORIGINS environment variable.
Step 6: Connect to Ollama on another PC
By default, the library talks to http://127.0.0.1:11434. To use a more powerful PC on your network, create a custom client:
import { Ollama } from 'ollama'
const ollama = new Ollama({ host: 'http://192.168.1.50:11434' })
const response = await ollama.chat({
model: 'llama3.1',
messages: [{ role: 'user', content: 'Hello from my laptop' }],
})
console.log(response.message.content)
Replace the address with your Ollama PC's IP address. That PC must be set up to accept network connections; our guide to sharing Ollama on your local network explains how. You can also pass custom headers if Ollama sits behind a proxy that needs them.
Tips
Already use the OpenAI SDK? Ollama also offers an OpenAI-compatible endpoint; see how to use Ollama's OpenAI-compatible API.
Prefer Python? Our Ollama Python guide covers the equivalent library.
Cloud models are available through the same library if you sign in to Ollama, but they run on Ollama's servers rather than your PC, so check what you're sending.
Our verdict
The official Ollama JavaScript library is the quickest way to add local AI to Node.js projects: npm i ollama, a few lines of code and your app is talking to a model on your own GPU. Streaming, model management and remote hosts are all built in, and the browser module covers front-end use.