How to beginner · 4 min read

How to use ONNX in JavaScript

Quick answer
Use onnxruntime-web or onnxruntime-node libraries to load and run ONNX models in JavaScript. These runtimes provide APIs to load models, prepare inputs, run inference, and get outputs efficiently in browsers or Node.js environments.

PREREQUISITES

  • Node.js 14+ or modern browser
  • npm or yarn package manager
  • Basic JavaScript/TypeScript knowledge

Setup

Install the onnxruntime-web package for browser or onnxruntime-node for Node.js. Prepare your ONNX model file accessible locally or via URL.

bash
npm install onnxruntime-web

# or for Node.js
npm install onnxruntime-node

Step by step

This example shows how to load an ONNX model and run inference in a browser environment using onnxruntime-web. It loads the model, creates input tensors, runs the session, and prints the output.

javascript
import * as ort from 'onnxruntime-web';

async function runOnnxModel() {
  // Load the ONNX model from URL or local path
  const session = await ort.InferenceSession.create('./model.onnx');

  // Prepare input tensor (example: float32 tensor with shape [1, 3])
  const inputTensor = new ort.Tensor('float32', new Float32Array([1.0, 2.0, 3.0]), [1, 3]);

  // Create feeds object with input name matching model's input
  const feeds = {};
  feeds[session.inputNames[0]] = inputTensor;

  // Run inference
  const results = await session.run(feeds);

  // Access output tensor by output name
  const output = results[session.outputNames[0]];
  console.log('Output tensor data:', output.data);
}

runOnnxModel();
output
Output tensor data: Float32Array([...])

Common variations

  • Use onnxruntime-node for Node.js with similar API but better performance.
  • Load models from URLs or local filesystem depending on environment.
  • Prepare inputs as ort.Tensor with correct data type and shape matching the model.
  • Use async/await for inference calls.
javascript
import * as ort from 'onnxruntime-node';

async function runNodeOnnx() {
  const session = await ort.InferenceSession.create('model.onnx');
  const inputTensor = new ort.Tensor('float32', new Float32Array([1, 2, 3]), [1, 3]);
  const feeds = {};
  feeds[session.inputNames[0]] = inputTensor;
  const results = await session.run(feeds);
  console.log('Node.js output:', results[session.outputNames[0]].data);
}

runNodeOnnx();
output
Node.js output: Float32Array([...])

Troubleshooting

  • If you see model not found, verify the model path or URL is correct and accessible.
  • For input shape errors, confirm input tensor shape matches model input signature.
  • Use browser developer console or Node.js logs to inspect errors.
  • Ensure your environment supports WebAssembly or WebGL for onnxruntime-web.

Key Takeaways

  • Use onnxruntime-web for browser and onnxruntime-node for Node.js to run ONNX models in JavaScript.
  • Always prepare input tensors with correct data type and shape matching the ONNX model inputs.
  • Load models asynchronously and handle inference with async/await for smooth execution.
  • Check model paths and input shapes carefully to avoid common runtime errors.
Verified 2026-04 · onnxruntime-web, onnxruntime-node
Verify ↗