skip to content

How do you make Transformers.js load models only from your own files, in a Node service and in a browser app, instead of from the Hugging Face Hub?

level: seniorimportance: should knowfreq 5%

answer

  1. cache, then local, then remote
  2. two allow flags with different defaults
  3. browser never looks locally by default
  4. Node's local path sits inside the package
  5. remote host can point at a mirror

basics

~20 s

Turn off remote loading with env.allowRemoteModels = false and point env.localModelPath at your files. In Node local loading is already on; in a browser you must also set env.allowLocalModels = true, or every load fails. env.remoteHost can target a mirror instead.

solid answer

~40 s

Transformers.js looks for each file in its cache, then under `env.localModelPath` plus the model id if `env.allowLocalModels` is on, then at `env.remoteHost` (default `https://huggingface.co/`) if `env.allowRemoteModels` is on. In a Node service, set `env.allowRemoteModels = false` and `env.localModelPath` to the directory baked into the image, because the default `/models/` resolves under the installed package; a missing file then throws at startup. In a browser, `allowLocalModels` defaults to `false`, so set it to `true` and serve the files under `/models/` on your origin; setting only `allowRemoteModels = false` throws because both sources are disabled. To keep lazy downloads but stay inside your network, point `env.remoteHost` at a mirror with the same `{model}/resolve/{revision}/` layout.

code

javascript · 14 lines
javascript
import { env, pipeline } from '@huggingface/transformers';

// Baked into the image at build time:
// /srv/models/your-org/your-model/config.json
// /srv/models/your-org/your-model/tokenizer.json
// /srv/models/your-org/your-model/tokenizer_config.json
// /srv/models/your-org/your-model/onnx/model.onnx   (fp32)
env.allowRemoteModels = false; // a missing file throws instead of downloading
env.localModelPath = '/srv/models/';

const extractor = await pipeline('feature-extraction', 'your-org/your-model', {
  device: 'cpu',
  dtype: 'fp32',
});

go deeper

for a junior

Recall the two switches, allowLocalModels and allowRemoteModels, and that localModelPath says where your own files live.

for a middle

Explain the lookup order (cache, local, remote), the different browser and Node defaults, and the file layout the library expects under localModelPath.

for a senior

Show a deployment that fails at startup on a missing file, ships exactly the files its device and dtype resolve to, and avoids the SPA-fallback and stale-cache traps.

for a principal

Weigh baking models into images against a private mirror with lazy fetching, counting image size, rollout speed, revision pinning and network policy.

## Where Transformers.js looks for a file Every file a model needs (`config.json`, `tokenizer.json`, the ONNX weights under `onnx/`) is fetched through one lookup routine. For each file it tries, in order: 1. **The cache** (the Cache API in a browser, the file-system cache in Node), if caching is on. A file cached earlier is served from there first. 2. **A local location**, but only when **`env.allowLocalModels`** is `true`. For a valid Hub-style id such as `your-org/your-model`, the path is `env.localModelPath` + model id + file name. Anything that is not a valid id, such as an absolute directory path, is used as the path directly. 3. **The remote host**, but only when **`env.allowRemoteModels`** is `true`: `env.remoteHost` + `env.remotePathTemplate` (default `'{model}/resolve/{revision}/'`) + file name. The defaults, as `env` defines them: | Setting | Default | Note | |---|---|---| | `allowRemoteModels` | `true` | the Hub is always a candidate unless you turn it off | | `remoteHost` | `'https://huggingface.co/'` | any host serving the same path layout works | | `allowLocalModels` | `false` in browsers, workers and Deno's web runtime; `true` elsewhere | the browser never looks locally unless told to | | `localModelPath` | `'/models/'` | in Node, resolved under the installed package's own directory, not your working directory | A local miss falls through to the remote host. With `allowRemoteModels` off, it raises a `ModelFileNotFoundError` naming the local path it tried instead. ## A Node service that must not download at runtime The goal is a service whose model files are baked into the image at build time, and that **fails at startup** rather than quietly downloading when one is missing. - Set **`env.allowRemoteModels = false`**. Any file not found locally now throws. - Set **`env.localModelPath`** to the directory you baked, for example `'/srv/models/'`. The default would point inside `node_modules`, which is rarely where you put models. - Lay the files out as the Hub does: `/srv/models/your-org/your-model/config.json`, `tokenizer.json`, `tokenizer_config.json` and the weights file for the device and dtype you run, such as `onnx/model.onnx` for `fp32` on `'cpu'`. - For one call instead of the whole process, `pipeline()` accepts **`local_files_only: true`**. - Alternatively, pass an absolute directory as the model argument. It is not a valid Hub id, so the library uses it as the path directly and never builds a Hub URL for it. Baking only the dtype you configure keeps the image small, but it ties the image to that dtype: change `dtype` or `device` and the expected file name changes too. ## A browser app that serves its own model files The browser default is the opposite: `allowLocalModels` is `false`, so the library never asks your server. - Set **`env.allowLocalModels = true`** and serve the files under `env.localModelPath` (by default the URL path `/models/`) on your own origin. - Optionally set **`env.allowRemoteModels = false`** so a missing file fails loudly instead of being fetched from the Hub. - Setting only `allowRemoteModels = false` is a common slip: with local loading still off, every load throws `Invalid configuration detected: both local and remote models are disabled.` One trap is specific to development servers. A local miss falls through to the remote host only when the local response is a **404**. A single-page-app server that answers unknown paths with `index.html` and a `200` hands the library an HTML page as if it were `config.json` or an ONNX file, and the failure surfaces as a parse error far from its cause. ## A mirror instead of the Hub When downloads must stay inside your network but you still want lazy fetching, point **`env.remoteHost`** at an internal mirror that serves the same `{model}/resolve/{revision}/` layout, or change **`env.remotePathTemplate`** to match the mirror's paths. The `revision` option still fills `{revision}`, so pinned revisions keep working. ## Checklist - Decide which of local, remote or mirror the deployment allows, and set both `allow*` flags explicitly. - Set `localModelPath` explicitly in Node. - Ship exactly the files the configured `device` and `dtype` resolve to. - Make missing files fail at startup, not on the first user request. - Remember the cache is checked first: in Node its key ignores the host, so files cached from the Hub are still served after you switch to a mirror.

  • Your dev server answers every unknown path with index.html. What goes wrong with local models?
    Transformers.js falls through to the remote host only when the local request returns 404. A 200 with the app's HTML is accepted as the file, so `config.json` or the ONNX weights become an HTML page and loading fails with a parse error. Serve `/models/` as static files that return a real 404.
  • A Node service switched env.remoteHost to a mirror, yet still uses files it downloaded from the Hub earlier. Why?
    The cache is checked before any local or remote lookup, and the Node file-system cache under `env.cacheDir` is keyed by model id and file name (plus the revision when it is not `main`), not by host. Hub downloads therefore keep being served. In a browser the Cache API entry is keyed by the full remote URL, so a new host misses it.

  • Setting allowRemoteModels = false alone makes a browser load from /models/.
  • In Node, the default localModelPath is ./models/ in the working directory.
  • env.remoteHost only accepts huggingface.co addresses.
  • A local 200 response with HTML is skipped and retried remotely.
  • Turning off allowRemoteModels also stops cached copies from being used.