transformers.js
ML inference for JavaScript, without a Python server. Supports text, vision, audio,
and multimodal tasks through a single pipeline() entry point.
Install
npm install @huggingface/transformers
Quick start
import { pipeline } from "@huggingface/transformers";
const classifier = await pipeline("sentiment-analysis");
const output = await classifier("I love transformers!");
// [{ label: "POSITIVE", score: 0.9998 }]
pipeline(task, model?, options?) is the one function you need 90% of the time.
Passing no model uses the default for that task.
Supported tasks
text-classification(alias:sentiment-analysis) — default model:Xenova/distilbert-base-uncased-finetuned-sst-2-englishtoken-classification(alias:ner) — default model:Xenova/bert-base-multilingual-cased-ner-hrlquestion-answering— default model:Xenova/distilbert-base-cased-distilled-squadfill-mask— default model:onnx-community/ettin-encoder-32m-ONNXsummarization— default model:Xenova/distilbart-cnn-6-6translation— default model:Xenova/t5-smalltext2text-generation— default model:Xenova/flan-t5-smalltext-generation— default model:onnx-community/Qwen3-0.6B-ONNXzero-shot-classification— default model:Xenova/distilbert-base-uncased-mnliaudio-classification— default model:Xenova/wav2vec2-base-superb-kszero-shot-audio-classification— default model:Xenova/clap-htsat-unfusedautomatic-speech-recognition(alias:asr) — default model:Xenova/whisper-tiny.entext-to-audio(alias:text-to-speech) — default model:onnx-community/Supertonic-TTS-ONNXimage-to-text— default model:Xenova/vit-gpt2-image-captioningimage-classification— default model:Xenova/vit-base-patch16-224image-segmentation— default model:Xenova/detr-resnet-50-panopticbackground-removal— default model:Xenova/modnetzero-shot-image-classification— default model:Xenova/clip-vit-base-patch32object-detection— default model:Xenova/detr-resnet-50zero-shot-object-detection— default model:Xenova/owlvit-base-patch32document-question-answering— default model:Xenova/donut-base-finetuned-docvqaimage-to-image— default model:Xenova/swin2SR-classical-sr-x2-64depth-estimation— default model:onnx-community/depth-anything-v2-smallfeature-extraction(alias:embeddings) — default model:onnx-community/all-MiniLM-L6-v2-ONNXimage-feature-extraction— default model:onnx-community/dinov3-vits16-pretrain-lvd1689m-ONNX
For full recipes — every task, grouped by modality, with runnable code —
see references/TASKS.md.
Choosing a model
Browse models compatible with transformers.js on the Hub: https://huggingface.co/models?library=transformers.js
Filter by task with the pipeline_tag parameter, e.g.
https://huggingface.co/models?library=transformers.js&pipeline_tag=text-generation.
Before recommending a model, confirm it actually has ONNX weights — the library cannot load a model without them. Two ways to check:
- Open the model page on the Hub and look for an
onnx/directory in the "Files and versions" tab. - Programmatically, with
ModelRegistry.get_available_dtypes(modelId)— returns the list of dtypes shipped. An empty array means the model exists but ships no ONNX files. It throws aModelFileNotFoundErrorif the model does not exist or is not accessible (private/gated — the Hub cannot tell these apart), and a regular error on network failures, so an unreachable model is never mistaken for one without ONNX files.
import { ModelRegistry, ModelFileNotFoundError } from "@huggingface/transformers";
// "Xenova/some-model" is a placeholder — substitute the ID you want to check.
try {
const dtypes = await ModelRegistry.get_available_dtypes("Xenova/some-model");
if (dtypes.length === 0) {
// Model exists but has no ONNX files — not usable with transformers.js.
}
} catch (e) {
if (e instanceof ModelFileNotFoundError) {
// Model does not exist, or is private/gated without a token.
} else {
throw e; // network failure — retry rather than blacklisting the model
}
}
Don't suggest a model without verifying this; the failure mode at runtime is a
download error that's harder to diagnose than a pre-flight check. For a fuller
pre-flight pattern (cache checks, dtype fallback), see
references/CONFIGURATION.md.
Quantization
Most pipelines accept a dtype option. Smaller dtypes download and run faster
at the cost of some accuracy:
dtype |
Size | Use when |
|---|---|---|
fp32 |
Largest | Maximum accuracy, Node.js with lots of RAM |
fp16 |
~50% of fp32 | GPU / WebGPU inference |
q8 |
~25% of fp32 | Good default for browsers |
q4 |
~12% of fp32 | Tight memory budgets, large language models |
q4f16 |
~12% of fp32 | Like q4 but with fp16 activations — pairs well with WebGPU LLMs |
const pipe = await pipeline("text-generation", "onnx-community/Qwen3-0.6B-ONNX", {
dtype: "q4",
});
Device
Default is CPU/WASM. Pass device: "webgpu" to run on the GPU when available:
const pipe = await pipeline("sentiment-analysis", null, { device: "webgpu" });
Memory management
Pipelines hold onto model weights and backend sessions. Always call
pipe.dispose() when you're done with one — especially in long-running
servers, before loading a replacement, or on component unmount.
const pipe = await pipeline("sentiment-analysis");
try {
const result = await pipe("Great!");
} finally {
await pipe.dispose();
}
Configuration
The env export lets
you control model sources, caching, logging, and the fetch function.
import { env, LogLevel } from "@huggingface/transformers";
env.allowRemoteModels = true;
env.useFSCache = true; // Node.js: cache downloaded models on disk
env.useBrowserCache = true; // Browser: cache via the Cache API
env.logLevel = LogLevel.WARNING;
See references/CONFIGURATION.md for the full
set of environment options, cache management, and private / gated models.
Pipeline options
Every pipeline accepts a progress_callback for download progress plus options
controlling device, dtype, and caching. The per-task recipes in
references/TASKS.md show common call options (e.g.
top_k, max_new_tokens) in use; shared loading options, generation
parameters, streaming, and KV-cache reuse are documented in
references/PIPELINE_OPTIONS.md. For the
exhaustive per-task option types, see the
API reference.
Things to never do
- Don't reuse a disposed pipeline. Create a new one with
pipeline(...)afterdispose(). - Don't recreate pipelines inside hot loops. Create once, call many times.
- Don't block startup on model downloads. Show progress via
progress_callback. - Don't fabricate model IDs. Confirm a model exists on the Hub and has ONNX files
(look for an
onnx/directory in the repo) before suggesting it to a user.
Reference documentation
- Official site: https://huggingface.co/docs/transformers.js
- API reference: https://huggingface.co/docs/transformers.js/api/pipelines
- Examples repo: https://github.com/huggingface/transformers.js-examples
This skill's local references:
TASKS.md— recipes for every task, grouped by modality (generated)CONFIGURATION.md—envoptions, caching, model inspectionPIPELINE_OPTIONS.md— common pipeline options, dtype, device, generation parameters