Get started with ORT for JavaScript
ONNX Runtime JavaScript API is the unified interface used by ONNX Runtime Node.js binding, ONNX Runtime Web, and ONNX Runtime for React Native.
Contents
ONNX Runtime Node.js binding
ONNX Runtime Node.js binding can be achieved by installing and importing.
Install
# install latest release version
npm install onnxruntime-node
Import
// use ES6 style import syntax (recommended)
import * as ort from 'onnxruntime-node';
// or use CommonJS style import syntax
const ort = require('onnxruntime-node');
Examples
- Follow the Quick Start instructions for ONNX Runtime Node.js binding.
Supported Versions
ONNX Runtime Node.js binding supports Node.js v12.x+ or Electron v5.x+
ONNX Runtime Web
You can install and import ONNX Runtime Web.
Install
# install latest release version
npm install onnxruntime-web
# install nightly build dev version
npm install onnxruntime-web@dev
Import
// use ES6 style import syntax (recommended)
import * as ort from 'onnxruntime-web';
// or use CommonJS style import syntax
const ort = require('onnxruntime-web');
If you want to use ONNX Runtime Web with WebGPU support (experimental feature), you need to import as below:
// use ES6 style import syntax (recommended)
import * as ort from 'onnxruntime-web/webgpu';
// or use CommonJS style import syntax
const ort = require('onnxruntime-web/webgpu');
Examples
ONNX Runtime Web can also be imported via a script tag in a HTML file, from a CDN server. Here are some examples:
- Quick Start (using bundler)
- Quick Start (using script tag)
- ONNX Runtime Web for In Browser Inference
- Inference in Javascript with ONNX Runtime Web
Supported Versions
ONNX Runtime supports mainstream modern browsers/OS on Windows, Ubuntu, macOS, Android, and iOS. You can check the compatibility of ONNX Runtime with modern browsers and operating systems for your desktop and mobile platforms. In-browser inference is possible with ONNX Runtime Web JavaScript that can enable cross-platform portability for web-applications.
ONNX Runtime for React Native
You can install and import ONNX Runtime Web for React Native.
Install
# install latest release version
npm install onnxruntime-react-native
Import
// use ES6 style import syntax (recommended)
import * as ort from 'onnxruntime-react-native';
// or use CommonJS style import syntax
const ort = require('onnxruntime-react-native');
Enable ONNX Runtime Extensions for React Native
To enable support for ONNX Runtime Extensions in your React Native app, you need to specify the following configuration as a top-level entry (note: usually where the package name
and version
fields are) in your project’s root directory package.json
file.
"onnxruntimeExtensionsEnabled": "true"
Builds
Builds are published to npm and can be installed using npm install
Package | Artifact | Description | Supported Platforms |
---|---|---|---|
Node.js binding | onnxruntime-node | CPU (Release) | Windows x64 CPU NAPI_v3, Linux x64 CPU NAPI_v3, MacOS x64 CPU NAPI_v3 |
Web | onnxruntime-web | CPU and GPU | Browsers (wasm, webgl), Node.js (wasm) |
React Native | onnxruntime-react-native | CPU | Android, iOS |
- For Node.js binding, to use on platforms without pre-built binaries, you can build Node.js binding from source and consume using
npm install <onnxruntime_repo_root>/js/node/
. - Consider the options and considerations for building a Web app with ONNX Runtime Web using JavaScript.
- Explore a simple web application to classify images with ONNX Runtime Web.
API Reference
See ONNX Runtime JavaScript API for API reference. Check out the ONNX Runtime Web demos! for image recognition, handwriting analysis, real-time emotion detection, object detection, and so on.
See also:
-
Typescript declarations for Inference Session, Tensor, and Environment Flags for reference.