Skip to content

How-ToGPU

How to Use Ollama with JavaScript: Run Local AI Models From Node.js

How to use Ollama from JavaScript: install the official library with npm, chat with a local model from Node.js, stream replies, manage models, use the browser module and connect to Ollama running on another PC.

By , Systems & Technical Editor

3 min read

How to Use Ollama with JavaScript: Run Local AI Models From Node.js

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.

The bottom line

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.

Sources

  1. 01Ollama, Ollama JavaScript Library READ
  2. 02Ollama, Local FAQ

About the author

Marcus Thorne

Systems & Technical Editor

Marcus writes Forge PC's step-by-step build, troubleshooting and system optimisation guides. His background spans Windows kernel and power-state diagnostics, UEFI/BIOS configuration, NVMe storage arrays, ATX 3.1 power delivery and running local AI and LLM workloads on consumer graphics cards.

More from Marcus Thorne

Read next

How to Fix a Mouse That Double-Clicks When You Click Once

How-To

How to Fix a Mouse That Double-Clicks When You Click Once

How to fix a mouse that double-clicks when you click once: check Windows' click and double-click speed settings, test in other apps, change USB port, clean the button and test on another PC, plus what to do if the mouse is faulty.

Marcus Thorne3 min

How to Use the Windows Backup App in Windows 11

How-To

How to Use the Windows Backup App in Windows 11

How to use the Windows Backup app in Windows 11 to save your folders to OneDrive and your apps, settings and Wi-Fi details to your Microsoft account, restore them on a new PC, change what's backed up and understand its limits.

Marcus Thorne3 min

How to Remove or Turn Off Copilot in Windows 11

How-To

How to Remove or Turn Off Copilot in Windows 11

How to remove or turn off Microsoft Copilot in Windows 11: uninstall the Copilot app in Settings or with PowerShell, change what the Copilot key does, and the AppLocker option Microsoft recommends for IT admins.

Marcus Thorne3 min

How to Translate Text Offline on Your PC with LibreTranslate

How-To

How to Translate Text Offline on Your PC with LibreTranslate

How to run LibreTranslate, a free and open-source translation service, on your own PC: install it with pip, load only the languages you need, translate in the browser or through its API, keep it local and add optional CUDA acceleration.

Marcus Thorne3 min