Preface¶
To perform sentiment analysis, image classification, or speech recognition in the frontend or Node, the common approach is to start a Python inference service or route requests to a cloud API. For teams using a pure JavaScript/TypeScript stack, this introduces additional deployment, key management, and network dependencies. Transformers.js itself can directly load pre-trained models from the Hub in browsers and Node.js, Bun, or Deno, using WebGPU or WASM for inference. In the official Skills repository, Hugging Face provides the transformers-js Skill, which writes conventions—such as the Pipeline API, task types, device and quantization options, and memory release—into a SKILL.md that Agents can follow. This facilitates coding assistants in environments like Cursor, Codex, and Claude Code to write on-device ML code using a consistent approach.
What is this¶
transformers-js is an Agent Skill within the Hugging Face skills repository (following the Agent Skills standard). Its metadata identifies the author as huggingface, the category as machine-learning, the license as Apache-2.0, and the underlying library as transformers.js.
The Skill is positioned to run NLP (text classification, translation, summarization, etc.), Computer Vision (image classification, object detection, etc.), Audio (speech recognition, audio classification, etc.), and multimodal tasks using Transformers.js in JavaScript/TypeScript. It supports browsers and Node.js, Bun, and Deno, utilizing pre-trained models from the Hugging Face Hub via WebGPU/WASM without the need for a separate Python service.
Runtime requirements (from the Skill description): Node.js 18+ (or compatible Bun/Deno), or a modern browser supporting ES modules; WebGPU requires runtime and hardware support, while WASM serves as a more universal fallback; fetching models from the Hub typically requires network access (local models can also be used).
Core Functions¶
Below are several key areas emphasized in the Skill documentation that align with the library’s capabilities.
Pipeline API¶
The Pipeline chains pre-processing, inference, and post-processing together and is the officially recommended way to get started. Create a pipeline corresponding to the task, pass in text or a media path, and obtain the result. After use, you must call dispose() to free memory; otherwise, leaks are likely to occur. The Skill highlights this with warnings in multiple places.
Models and Tasks¶
The second parameter can specify a model ID on the Hub (e.g., the multilingual sentiment analysis model Xenova/bert-base-multilingual-uncased-sentiment). The documentation provides Hub links to filter models by library=transformers.js and pipeline_tag to facilitate selection based on the task.
Device and Quantization¶
You can select the runtime device via options, such as using the GPU with device: 'webgpu'; control precision via dtype (options listed in the documentation include fp32, fp16, q8, q4), balancing size and speed.
Supported Task Range (Excerpt)¶
The Skill includes minimal examples for various tasks, covering:
- NLP: Text classification, Named Entity Recognition (
token-classification), Question Answering, Text Generation, Translation, Summarization, Zero-shot Classification, etc. - Vision: Image Classification, Object Detection, Segmentation, Depth Estimation, Zero-shot Image Classification, etc.
- Audio: Automatic Speech Recognition, Audio Classification, Text-to-Speech, etc.
- Multimodal: Image Captioning (image-to-text), Document Question Answering, Zero-shot Object Detection, etc.
For more in-depth content such as text generation, streaming output, and conversational formats, the Skill points to reference documents like references/TEXT_GENERATION.md within the repository.
Installation and Enablement¶
Understand this in two layers: installing the Transformers.js library (for writing business code) and installing the Agent Skill into your coding assistant (so the Agent writes code according to the Skill).
Installing the npm Package¶
Install the official package in Node or bundled frontend projects:
npm install @huggingface/transformers
Browsers can also import it as an ES module via CDN (the Skill example uses jsDelivr):
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers';
</script>
Enabling the Skill in Agent Environments¶
Hugging Face Skills are compatible with Claude Code, Codex, Gemini CLI, Cursor, etc. According to the repository instructions, the Cursor marketplace defaults to focusing on hf-cli; to install other workflow skills, you can use the Hugging Face CLI:
hf skills add transformers-js
Claude Code users can first register the marketplace and then install the CLI skill, using hf skills add <skill-name> to add transformers-js. Codex users can copy or symlink the skills/transformers-js directory to the .agents/skills path convention required by Codex. Please refer to the current documentation at huggingface/skills for specific steps.
Skill path:
https://github.com/huggingface/skills/tree/main/skills/transformers-js
Typical Usage¶
Sentiment Analysis (Minimal Closed Loop)¶
The three-step pattern of creating a pipeline, running inference, and releasing resources is repeatedly emphasized in the documentation:
import { pipeline } from '@huggingface/transformers';
const pipe = await pipeline('sentiment-analysis');
const result = await pipe('I love transformers!');
// Output: [{ label: 'POSITIVE', score: 0.999817686 }]
await pipe.dispose();
Specifying Model and WebGPU¶
const pipe = await pipeline(
'sentiment-analysis',
'Xenova/bert-base-multilingual-uncased-sentiment',
{
device: 'webgpu',
}
);
Quantized Models¶
const pipe = await pipeline('sentiment-analysis', 'model-id', {
dtype: 'q4',
});
Other Task Examples¶
Question Answering:
const qa = await pipeline('question-answering');
const answer = await qa({
question: 'What is the capital of France?',
context: 'Paris is the capital and largest city of France.'
});
Automatic Speech Recognition:
const transcriber = await pipeline('automatic-speech-recognition');
const result = await transcriber('audio.wav');
For image classification, you can pass an image URL or local path (specific input formats depend on the current library documentation). For a more complete list of tasks and parameters, see the SKILL.md and references/ directory in the Skill repository.
Applicable Scenarios and Cautions¶
Who it is for
- JS/TS developers who need to perform client-side inference in the browser and reduce backend ML services.
- Teams performing batch processing or lightweight inference on the Node.js, Bun, or Deno side, hoping to source models from the Hugging Face Hub.
- Scenarios where you want Agents to generate Transformers.js code that consistently follows dispose, device, and quantization conventions, rather than guessing the API from scratch every time.
Limitations and Cautions
- The first use requires downloading models from the Hub (size varies by task); network access is required or local models must be prepared in advance.
- WebGPU is not available in all environments; when unsupported, you should rely on the WASM/CPU path.
- Large models are limited by memory and computing power on the device side, so the
dtypeand model selection in the Skill are very important. - The Agent Skill is an operational guide and does not replace reading the release notes of transformers.js and the model cards on the Hub.
Conclusion¶
transformers-js solidifies the Pipeline usage, task coverage, and operational details (especially pipe.dispose()) of Transformers.js in the browser and Node ecosystems into instructions that Agents can execute. It is suitable for teams focused on on-device AI and Web ML where the primary stack is JavaScript. Official Skill address:
https://github.com/huggingface/skills/tree/main/skills/transformers-js