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.
Create ask.js:
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)
Run it:
node ask.js
Check response.ok before parsing a success payload. A local service can be unavailable, return a malformed request error, or reject an unknown model.
Keep the URL and model name in configuration when the project grows. Do not scatter them across every feature.
Lesson completed