krisk/Fuse · error · Error

Token search is not available

Error message

Token search is not available

What it means

Fuse.js gates token search behind the TOKEN_SEARCH_ENABLED environment variable. Constructing a Fuse instance with useTokenSearch: true without that env var set makes the constructor throw before any indexing. The feature code is excluded from builds that never enable the flag.

Source

Thrown at src/core/index.ts:80

    options?: IFuseOptions<any>
  ) => SearchResult

  constructor(
    docs: ReadonlyArray<T>,
    options?: IFuseOptions<T>,
    index?: FuseIndex<T>
  ) {
    this.options = { ...Config, ...options } as Required<IFuseOptions<T>>

    if (
      this.options.useExtendedSearch &&
      !process.env.EXTENDED_SEARCH_ENABLED
    ) {
      throw new Error(ErrorMsg.EXTENDED_SEARCH_UNAVAILABLE)
    }

    if (this.options.useTokenSearch && !process.env.TOKEN_SEARCH_ENABLED) {
      throw new Error(ErrorMsg.TOKEN_SEARCH_UNAVAILABLE)
    }

    this._keyStore = new KeyStore(this.options.keys)

    this._docs = docs as T[]
    this._myIndex = null as any
    this._invertedIndex = null

    this.setCollection(docs, index)

    this._lastQuery = null
    this._lastSearcher = null
  }

  _getSearcher(query: string): Searcher {
    if (this._lastQuery === query) {
      return this._lastSearcher!
    }

View on GitHub (pinned to edf2fb608e)

Solutions

  1. Set process.env.TOKEN_SEARCH_ENABLED to a truthy value ('1') before instantiating Fuse.
  2. Remove useTokenSearch: true if token search is not required.
  3. Verify dotenv or platform env config loads the variable in every environment where Fuse is constructed.

Example fix

// before
const fuse = new Fuse(docs, { keys: ['name'], useTokenSearch: true })
// after
process.env.TOKEN_SEARCH_ENABLED = '1'
const fuse = new Fuse(docs, { keys: ['name'], useTokenSearch: true })
Defensive patterns

Strategy: validation

Validate before calling

if (options.useTokenSearch && !process.env.TOKEN_SEARCH_ENABLED) {
  throw new Error('Set TOKEN_SEARCH_ENABLED before using useTokenSearch')
}
const fuse = new Fuse(docs, options)

Type guard

const tokenSearchReady = () => Boolean(process.env.TOKEN_SEARCH_ENABLED)

Try / catch

try {
  return new Fuse(docs, options)
} catch (e) {
  if (e.message === 'Token search is not available') {
    process.env.TOKEN_SEARCH_ENABLED = '1'
    return new Fuse(docs, options)
  }
  throw e
}

Prevention

When it happens

Trigger: Calling new Fuse(docs, { keys: [...], useTokenSearch: true }) while process.env.TOKEN_SEARCH_ENABLED is unset or falsy at construction time.

Common situations: Deployments where .env is not loaded; enabling token search after copying config from a project with the env var set; CI/staging environments missing the flag present in local dev.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of krisk/Fuse@edf2fb608e (2026-09-02). Data as JSON: /api/errors/9a1e2e2439308825. Report an issue: GitHub.