Run models locally
Call Ollama from Node.js
Use the built-in fetch API to connect a Node.js program to the local model without adding an SDK.
Node includes fetch(), so the simplest client needs no dependency.
Keep the script at the top level or wrap it in an async function if your Node version requires it:
async function main() {
const response = await fetch('http://localhost:11434/api/chat', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
model: 'gemma3:1b',
messages: [
{ role: 'user', content: 'Explain local inference in one sentence.' },
],
stream: false,
}),
})
if (!response.ok) {
throw new Error(`Ollama returned ${response.status}`)
}
const data = await response.json()
console.log(data.message.content)
}
main().catch(console.error)
Run it:
node ask.js
You should see one sentence printed to the terminal. The exact wording varies. The important part is a 200 response and non-empty message.content.
Check response.ok before parsing a success payload. A local service can be unavailable, return a malformed request error, or reject an unknown model. A 404 on an unknown model tag is common when you typo the name.
Keep the URL and model name in configuration when the project grows. Do not scatter them across every feature. One config.json or environment block saves refactors when you pin a new tag.
Wrap the call in a small function so tests can stub it. Your application cares about { summary, confidence }, not about Ollama’s response shape.
If node ask.js throws fetch failed, Ollama is not running. If it throws Ollama returned 404, pull the model or fix the tag. Those two errors should surface different user messages in a real app.
Add a minimal error wrapper so callers see useful messages:
try {
const text = await askOllama('Explain local inference in one sentence.')
console.log(text)
} catch (error) {
console.error('Local model unavailable:', error.message)
}
When Ollama is stopped, you should see Local model unavailable: instead of a generic stack trace in user-facing logs.
Try this on your own project: copy the script, run it twice with Ollama running, then stop Ollama and run it again. Confirm the second warm call is often faster, and the stopped run fails with a clear message you can map to UI copy.
Lesson completed